modo-studio / SugarRecord

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

Update must run save() two times #317

Open zaneLou opened 7 years ago

zaneLou commented 7 years ago

What

jsonEntity is NSManagedObject, when i got it from database, update some property. if i commet try (self.db.mainContext as? NSManagedObjectContext)?.save(), database is not updated, when i restart my app, jsonEntity's property is not updated. i need run two save() slove the problem.

Context

        do {
            jsonEntity?.content = self._gameProfileModel?.toJSONString()
            try db.operation ({ (context, save) throws in
                try (self.db.mainContext as? NSManagedObjectContext)?.save()
                save()
            })
        }
        catch {
            print("updateGameProfileModel error \(error)")
        }
pepicrft commented 7 years ago

Hi @zaneLou calling save() using the closure given in the operation call should be enough. Don't get the db main context and save it manually because the operation is performed in a different context. Once the context is saved you should get the changes in the main context. Try registering an observer in that context and checking if you get a notification after your models get saved. Let me know if that helps.

zaneLou commented 7 years ago

@pepibumur maybe you know i mean. if just run save(), I can find the change, the data is updated. but when i restart my app, the data is restored, and change not happened. very curiously. i use xcode 8.2. you can do some test, change the data, and restart app, you can find it.

pepicrft commented 7 years ago

@zaneLou have you tried with the example app that is provided. Because it does exactly what you're trying to do.

zaneLou commented 7 years ago

@pepibumur i can't run your sample. you do not know CocoaPods and Carthage much depends on network. Skipped downloading realm-cocoa.framework binary due to the error: "API rate limit exceeded for 118.144.133.36. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)" Checking out realm-cocoa at "v1.1.0" *** Skipped downloading SugarRecord.framework binary due to the error: "API rate limit exceeded for 118.144.133.36. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)"

and I find your Basic of Example, and not find create a model and update it, and restart again.

pepicrft commented 7 years ago

@zaneLou what do you mean by:

do not know CocoaPods and Carthage much depends on network.

pepicrft commented 7 years ago

Hey @zaneLou, is this issue still happening to you? Otherwise, I'll close the issue in a couple of on April 10th.

aemgtz commented 7 years ago

I have the same problem. Update is doesn't work.

pepicrft commented 7 years ago

@aemgtz do you get any error in the catch statement?

BigToeProductions commented 5 years ago

I am having a similar issue and am not receiving an error in the catch statement.

This does not save

        do {
            try db.operation {(context, save) throws in
                let users = try db.fetch(FetchRequest<MyUser>())
                if users.count > 0 {
                    let user = users.first
                    user?.fname = "Batman"
                    save()
                }
            }
        } catch {
            print (error)
        }

But this does save successfully

        do {
            try db.operation {(context, save) throws in
                let users = try db.fetch(FetchRequest<MyUser>())
                if users.count > 0 {
                    let user = users.first
                    user?.fname = "Batman"
                    try user?.managedObjectContext?.save()
                    save()
                }
            }
        } catch {
            print (error)
        }

I just took over this project which is using SugarRecord so I am trying to get up to speed. i would love to know if it is still being supported.