Cancel Edit Open Realm.io Database

I want to create an editing view for an existing object model in a Realm.io database. The view controller has a save button that should save the changes and a cancel button that should cancel the changes.

I cannot change RLMObject outside of a write transaction, so what is the recommended RLMObject temporary change RLMObject allows me to roll back changes later if necessary?

+5
source share
1 answer

You can transfer your realm object to your edit view controller as an object in memory for editing. For instance:

 RLMRealm *realm = [RLMRealm defaultRealm]; [realm beginWriteTransaction]; [StringObject createInDefaultRealmWithObject:@[@"a"]]; [realm commitWriteTransaction]; StringObject *obj = [[StringObject alloc] initWithObject:[[StringObject allObjects] firstObject]]; XCTAssertEqualObjects(obj.stringCol, @"a"); obj.stringCol = @"b"; // not in a write transaction XCTAssertEqualObjects(obj.stringCol, @"b"); 

If the user clicks "Save", you can call createOrUpdateInDefaultRealmWithObject: and pass your object in memory, which will then pass all the values ​​and update this object in Realm. Note that your object must have a primary key for this.

If the user clicks Cancel, you can simply discard this object in memory as if nothing had happened.

Please note that in the future we intend to add transaction rollback functionality, which will simplify this template.

+1
source

Source: https://habr.com/ru/post/1204654/


All Articles