2015-04-24 8 views
24

Vorrei consegnare la mia app con dati già precompilati nel mio database di dominio. Devo semplicemente copiarlo nella directory dei documenti o ci sono altre cose da fare? documentazioneCome recapitare l'app con il database di realm precompilato

+0

Android/iOS o entrambi? – caulitomaz

+0

iOS: fa la differenza? – swalkner

+0

Sì. @jpsim ti indirizza alla documentazione per iOS (Objective-C). http://stackoverflow.com/questions/26240286/use-pre-populated-databases-with-realm punta a una soluzione più cross-environment utilizzando Realm.getInstance() di un file * .realm. – caulitomaz

risposta

27

di Realm ha una sezione sulla "Bundling a Realm with an App":

It’s common to seed an app with initial data, making it available to your users immediately on first launch. Here’s how to do this:

  1. First, populate the realm. You should use the same data model as your final, shipping app to create a realm and populate it with the data you wish to bundle with your app. Since realm files are cross-platform, you can use an OS X app (see our JSONImport example) or your iOS app running in the simulator.

  2. In the code where you’re generating this realm file, you should finish by making a compacted copy of the file (see -[RLMRealm writeCopyToPath:error:]). This will reduce the Realm’s file size, making your final app lighter to download for your users.

  3. Drag the new compacted copy of your realm file to your final app’s Xcode Project Navigator.

  4. Go to your app target’s build phases tab in Xcode and add the realm file to the “Copy Bundle Resources” build phase.

  5. At this point, your bundled realm file will be accessible to your app. You can find its path by using [[NSBundle mainBundle] pathForResource:ofType:].

  6. You can either create a read-only realm by calling [RLMRealm realmWithPath:readOnly:error:]. Or, if you’d like to create a writable realm file based on this initial data, you can copy the bundled file to your application’s Documents directory using [[NSFileManager defaultManager] copyItemAtPath:toPath:error:] and then construct your new realm by using [RLMRealm realmWithPath:].

You can refer to our migration sample app for an example of how to use a bundled realm file.

+3

E le istruzioni per Android? La documentazione per Java manca di una sezione simile. – caulitomaz

+3

È possibile visualizzare un esempio di come raggruppare un file di Realm nell'esempio di migrazione Java qui: https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/ reame/examples/realmmigrationexample/MigrationExampleActivity.java. –

+1

Vedere la mia [risposta] (http://stackoverflow.com/a/36850732/262789) per il modo supportato da Realm Android di aggiungere dati iniziali. – Benjamin

1

pre riempita Realm-database per Android

Mettere il database regno in res/cartella prime

ed eseguire codice riportato di seguito attività:

// Copia del database di reame

copyBundledRealmFile(this.getResources().openRawResource(R.raw.default0), "default0.realm"); 

RealmConfiguration config0 = new RealmConfiguration.Builder() 
      .name("default0.realm") 
      .build(); 

realm = Realm.getInstance(config0); 

private String copyBundledRealmFile(InputStream inputStream, String outFileName) { 
    try { 
     File file = new File(this.getFilesDir(), outFileName); 
     FileOutputStream outputStream = new FileOutputStream(file); 
     byte[] buf = new byte[1024]; 
     int bytesRead; 
     while ((bytesRead = inputStream.read(buf)) > 0) { 
      outputStream.write(buf, 0, bytesRead); 
     } 
     outputStream.close(); 
     return file.getAbsolutePath(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 
Problemi correlati