The best way would likely opening the bundled prefilled database as read-only.{#HS:191114076-3758#}
You can then retrieve all the objects, iterate over them and assign each a new UUID and add them to the Realm.let resourcePath = NSBundle.mainBundle().resourcePath as NSString? let readonlyRealmPath = resourcePath!.stringByAppendingPathComponent(“default.realm”) let readonlyRealm = try! Realm(path: readonlyRealmPath) let realm = try! Realm() var objects = readonlyRealm.objects(MyObj) for object in objects { object.uuid = NSUUID().UUIDString realm.create(MyObj.self, value: object) }
--
Marius Rackwitz
iOS Product Engineer
--
You received this message because you are subscribed to the Google Groups "Realm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-cocoa...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-cocoa/111fc38f-6563-44be-a37d-6f33d68bce73%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Both alternatives are functionality equivalent. Dictionaries may make it easier in some cases to apply dynamic programming strategies (e.g. iterating over all properties) while unattached objects can help to to enforce the correct types. Beside that dictionaries can be used to describe only the primary key and new attributes on update, but that doesn't help here.
--
Marius Rackwitz
iOS Product Engineer
{#HS:191114076-3758#}
On Tue, Apr 12, 2016 at 12:06 PM UTC, Ivan Schuetz <ivans...@gmail.com> wrote:
Ah, just received your message. Will do it like this then if it's the only way. Btw what is the difference between using a dictionary/create and initialising new database objects/add?
On Tue, Apr 12, 2016 at 12:01 PM UTC, Ivan Schuetz <ivans...@gmail.com> wrote:
I could create new instances and copy all the fields from the read only object but this means everything is loaded into memory. Maybe there's a better way?
On Tue, Apr 12, 2016 at 12:00 PM UTC, Marius Rackwitz <he...@realm.io> wrote:Oops! Totally missed that when writing the sample code.
You will need to create instead a modified intermediate representation of the object in memory.
That could be for example a dictionary, which you can directly pass to Realm#create.dict = [ "uuid": NSUUID().UUIDString, "propA": object.propA, "propB": object.propB, ] realm.create(MyObj.self, value: dict)
It really depends on the structure of your data and how much modifications, you would need to make.
But if shipping the code you use to generate the prefilled database is an option, that would slim down the resulting app size and so also beneficial to your users in that way.
--
Marius Rackwitz
iOS Product Engineer
{#HS:191114076-3758#}
On Tue, Apr 12, 2016 at 12:45 PM UTC, Ivan Schuetz <ivans...@gmail.com> wrote:
I just noticed another problem. I have 2 prefill types A and B, A has B as a linked property. Since I'm creating new instances of B it looks like I have to update A to reference these new instances. So I'd have probably to create a look up dictionary of the new Bs to retrieve them while creating the A instances/dictionaries.
I wonder if I maybe should ship the prefill database in code rather than as a Realm. The reason I used the Realm was the performance benefit of just having to copy the file, but now it seems that, since I have to load it and iterate through all the entries the performance may be worser and this lookup makes the code also more complex.
On Tue, Apr 12, 2016 at 12:34 PM UTC, Marius Rackwitz <he...@realm.io> wrote:
Both alternatives are functionality equivalent. Dictionaries may make it easier in some cases to apply dynamic programming strategies (e.g. iterating over all properties) while unattached objects can help to to enforce the correct types. Beside that dictionaries can be used to describe only the primary key and new attributes on update, but that doesn't help here.
--
Marius Rackwitz
iOS Product Engineer