modo-studio / SugarRecord

CoreData/Realm sweet wrapper written in Swift
MIT License
2.11k stars 223 forks source link

Updating Realm object without primary key? #254

Open Tj3n opened 8 years ago

Tj3n commented 8 years ago

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?

pepicrft commented 8 years ago

Good idea @Tj3n we'll add it.

thepelican commented 8 years ago

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) }

Tj3n commented 8 years ago

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

thepelican commented 8 years ago

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

Tj3n commented 8 years ago

@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

pepicrft commented 8 years ago

@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 😛

Tj3n commented 8 years ago

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

thepelican commented 7 years ago

this issue its been closed but its not being resolved