Skip to content
This repository has been archived by the owner on Feb 28, 2020. It is now read-only.

Save data but crashed at try super.save() #91

Closed
gizcloud opened this issue Oct 15, 2015 · 13 comments
Closed

Save data but crashed at try super.save() #91

gizcloud opened this issue Oct 15, 2015 · 13 comments

Comments

@gizcloud
Copy link

When I try to save data with Swift2.0. The program broke down at , When I ran again, the data has been saved into the database when fetch them. how to solve that?

  public override func save() throws {
        guard self.hasChanges else { return }

        try super.save()   <------------- This point

        var error: ErrorType? = nil
        self.rootSavingDataContext.performBlockAndWait {
            self.enableMergeFromRootSavingDataContext = false
            defer { self.enableMergeFromRootSavingDataContext = true }

            do {
                try self.rootSavingDataContext.save()
            }
            catch let innerError {
                error = innerError
            }
        }

        if let error = error {
            throw error
        }
    }

My Code is:

  @IBAction func doCreate(sender: AnyObject) {

        let dataContext = DataContext();
        let gang = dataContext.gangs.createEntity();
        gang.nick = randomStringWithLength(5);

        gang.nick_py = randomStringWithLength(5);


        do {
           try dataContext.save()
        }
        catch let error {
           print("data saved error \(error)")
        }
}



    }
@vmartinelli
Copy link
Member

Hello. Do you have a description of the innerError?

@gizcloud
Copy link
Author

Image of Captured Sreen

@ssxenon01
Copy link

Hi @gizcloud did you find solution? I'm also getting this error 👍

@vmartinelli
Copy link
Member

Without a description of the error is very difficult to say something. The screenshot you posted does not show it.

@vmartinelli
Copy link
Member

I mean I need to know what is the result of the print.

@ssxenon01
Copy link

There where no error log. Just crash with pointer.
In my case:Error was I initialized DataContext() 2 times in 2 different controller. When i changed it to one. Error has gone.

@thellimist
Copy link

I had a similar bug.

let album = dataContext.collectionAlbums.filter({ $0.text ==  text}).first
album.user = user
if album != nil { // Adding this block fixed the bug
  do {
   try dataContext.save()
  }
  catch let error {
   QL4Error("error \(error)")
  }
}


@waltermvp
Copy link
Contributor

Same issue with me the print never executes

@waltermvp
Copy link
Contributor

Update:
Passing around one DataContext object instead of creating them over again solves this issue. Does is make sense to create a singleton function? @vmartinelli

@amayatsky
Copy link

I have the same issue which is resolved when I use static DataContext

@vmartinelli
Copy link
Member

Well... Usually only one main thread context is used by an app. Other background contexts can be used as well (temporary or not). Anyway, can you test if this issue occurs using the version from develop branch as well?

@lie-an
Copy link

lie-an commented Mar 4, 2016

Hello, I'm having the same error.

I have tested using the version from develop branch as well.

I am saving JSON data (multiple entities) concurrently and the app crash in

try super.save()

inside DataContext.swift.

@amayatsky
Copy link

I had that problem too and ended up using a static instance:

extension DataContext {
    class var sharedInstance: DataContext {
        struct Static {
            static let instance: DataContext = DataContext()
        }
        return Static.instance
    }
}

I've also moved the saving to a single place, since I use the ACD for a cache only and don't care much about validity of the stored data:

func applicationDidEnterBackground(application: UIApplication) {
        do {
            try DataContext.sharedInstance.save()
            print("applicationDidEnterBackground data saved")
        } catch {
            print("applicationDidEnterBackground:\(error)")
        }   
    }

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

No branches or pull requests

7 participants