Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update must run save() two times #317

Open
zaneLou opened this issue Dec 20, 2016 · 9 comments
Open

Update must run save() two times #317

zaneLou opened this issue Dec 20, 2016 · 9 comments

Comments

@zaneLou
Copy link

zaneLou commented Dec 20, 2016

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
Copy link

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
Copy link
Author

zaneLou commented Dec 25, 2016

@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
Copy link

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

@zaneLou
Copy link
Author

zaneLou commented Dec 29, 2016

@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
Copy link

@zaneLou what do you mean by:

do not know CocoaPods and Carthage much depends on network.

@pepicrft
Copy link

pepicrft commented Apr 7, 2017

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

@aemgtz
Copy link

aemgtz commented Jul 10, 2017

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

@pepicrft
Copy link

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

@pepicrft pepicrft removed their assignment May 2, 2018
@BigToeProductions
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants