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
risposta
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:
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.
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.
Drag the new compacted copy of your realm file to your final app’s Xcode Project Navigator.
Go to your app target’s build phases tab in Xcode and add the realm file to the “Copy Bundle Resources” build phase.
At this point, your bundled realm file will be accessible to your app. You can find its path by using [[NSBundle mainBundle] pathForResource:ofType:].
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.
E le istruzioni per Android? La documentazione per Java manca di una sezione simile. – caulitomaz
È 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. –
Vedere la mia [risposta] (http://stackoverflow.com/a/36850732/262789) per il modo supportato da Realm Android di aggiungere dati iniziali. – Benjamin
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;
}
- 1. Controllare due campi con il database Realm
- 2. Ho usato Singleton con Realm Database corretto?
- 3. Realm per nativo di React. reset database
- 4. Database predefinito di Realm Android clear
- 5. Realm - come chiudere l'istanza del realm con una transazione asincrona
- 6. Con MvvmCross qual è il modo preferito per copiare un database SQLite precompilato
- 7. Distribuire il modulo di estensione python precompilato con le distutils
- 8. utilizzare tensorflow precompilato con cmake
- 9. Come eliminare un oggetto da Realm Database Android?
- 10. Come posso creare un database di Realm con dati iniziali per la mia app per Android?
- 11. Swift - Database SQLite precompilato non incluso nel dispositivo
- 12. Realm Pulizia di vecchi oggetti
- 13. Come impostare il "realm" usando PHP OAuth?
- 14. Realm: crea relazioni con identificativi
- 15. Come utilizzare correttamente Realm
- 16. Come utilizzare le notifiche di Realm
- 17. copia il file di Realm nella directory di Caches
- 18. Devo preoccuparmi di chiudere il database di Realm in Objective-C e Swift?
- 19. Come utilizzare Realm asObservable con l'operatore concat() di RxJava?
- 20. Realm Android - Passaggio dell'oggetto Realm tramite Intent
- 21. Utilizzare un tema precompilato
- 22. Come si avvia l'app di posta elettronica con il campo "a" precompilato?
- 23. Come utilizzare Realm Swift Init
- 24. Realm java sorta con più campi
- 25. EmberJS - registro manubrio precompilato modello
- 26. Realm java condizioni di query
- 27. Come posso rendere Tomcat precompilato JSP all'avvio?
- 28. Realm data-query
- 29. Realm su Windows 10
- 30. Rails assets: comportamento anomalo precompilato
Android/iOS o entrambi? – caulitomaz
iOS: fa la differenza? – swalkner
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