SugarRecord icon indicating copy to clipboard operation
SugarRecord copied to clipboard

Updating Realm object without primary key?

Open Tj3n opened this issue 8 years ago • 8 comments

Hi, I'd like to ask what's the correct way to update a Realm Object that doesn't have primary key in 1 closure? I tried remove + insert but it gave me error, i have to change the insert function to update: false only it works, maybe should include another function for insert without primary key?

Tj3n avatar May 23 '16 05:05 Tj3n

Good idea @Tj3n we'll add it.

pepicrft avatar May 23 '16 09:05 pepicrft

on the same topic, looks like we cant use update: true in teh insert action... you guys found any way? The only solution is to remove and then add teh field. Does it mean that realm will interpret it as modification? or like two different actions?

if it was the latter it will never show up in modifications section in the code below: case .Update(let objects, let deletions, let insertions, let modifications): let deletions = deletions.map { $0 } let insertions = insertions.map { (index: $0, element: objects[$0]) } let modifications = modifications.map { (index: $0, element: objects[$0]) } return ObservableChange.Update(deletions: deletions, insertions: insertions, modifications: modifications) }

thepelican avatar Aug 05 '16 06:08 thepelican

I end up change the whole insert function with the update: Bool added, so that it will use that parameter for the Realm one inside, cant find extension solution for that since the function is based on extension and protocol already

Tj3n avatar Aug 05 '16 09:08 Tj3n

@Tj3n which branch are you using? Cant really find this mantioned change anywhere

thepelican avatar Aug 08 '16 01:08 thepelican

@thepelican it haven't been implemented yet in the lib, i changed it for my own use only, this depends on the author, he might create another function for that

Tj3n avatar Aug 08 '16 05:08 Tj3n

@Tj3n feel free to propose your solution. I'm more than open to discuss it and merge it into the repository. I haven't started working on it yet 😛

pepicrft avatar Aug 08 '16 09:08 pepicrft

This is actually a critical for Realm user but it's pretty easy to fix, since CoreData's NSManagedObjectContext: Context doesnt seems to do anything with the insert function, i just change it to func insert<T: Entity>(entity: T, update: Bool) throws and use the update param to pass to the Realm's add function inside it

Tj3n avatar Aug 08 '16 09:08 Tj3n

this issue its been closed but its not being resolved

thepelican avatar Nov 18 '16 06:11 thepelican