Getting a serious fault when trying to pass in an entity object from one ViewController to another. #106

Closed
opened 2025-12-29 15:24:44 +01:00 by adam · 4 comments
Owner

Originally created by @ghost on GitHub (Dec 8, 2016).

So i have a button in the first view controller that when pressed, creates an entity friend like so:

       Static.datastack.beginAsynchronous{  (transaction) in
        
        
        let json: [String: AnyObject] = [ "id" : ID as AnyObject]
        
        let friend = try! transaction.importUniqueObject(Into(Friend.self), source: json)
        
        
        transaction.commit({ (saveResult) in
            
       if saveResult.boolValue == true {
        let secondvc = secondvc()
        secondvc.friend = friend   //this is the part that fails
        self.navigationController?.show(secondvc, sender: nil)
        }
        })
        
        
    }

class secondvc() {

var friend: Friend!

}

In the second view controller, if I try to access the value friend, its nil. I 'am absolutely positive that it is indeed saving since I can access the value in the first view controller (if i were to print it in the commit completion handler, it would give a value, but in the second vc, its nil). I have found a workaround to this which is just to fetch the friend in the second view controller. However, I 'am very curious as to what the problem is and how to go about fixing it. This framework is so powerful that I want to know how to fully manipulate it. Thank you !

Originally created by @ghost on GitHub (Dec 8, 2016). So i have a button in the first view controller that when pressed, creates an entity friend like so: Static.datastack.beginAsynchronous{ (transaction) in let json: [String: AnyObject] = [ "id" : ID as AnyObject] let friend = try! transaction.importUniqueObject(Into(Friend.self), source: json) transaction.commit({ (saveResult) in if saveResult.boolValue == true { let secondvc = secondvc() secondvc.friend = friend //this is the part that fails self.navigationController?.show(secondvc, sender: nil) } }) } class secondvc() { var friend: Friend! } In the second view controller, if I try to access the value friend, its nil. I 'am absolutely positive that it is indeed saving since I can access the value in the first view controller (if i were to print it in the commit completion handler, it would give a value, but in the second vc, its nil). I have found a workaround to this which is just to fetch the friend in the second view controller. However, I 'am very curious as to what the problem is and how to go about fixing it. This framework is so powerful that I want to know how to fully manipulate it. Thank you !
adam closed this issue 2025-12-29 15:24:45 +01:00
Author
Owner

@ghost commented on GitHub (Dec 8, 2016):

And i forgot, a fault does print out when passing friend to the second view controller when I try to print(self.friend) in the second vc

@ghost commented on GitHub (Dec 8, 2016): And i forgot, a fault does print out when passing friend to the second view controller when I try to print(self.friend) in the second vc
Author
Owner

@JohnEstropia commented on GitHub (Dec 8, 2016):

Hi, you are passing a transaction-only instance which would be invalid after commit().

try

secondvc.friend = Static.datastack.fetchExisting(friend)
@JohnEstropia commented on GitHub (Dec 8, 2016): Hi, you are passing a transaction-only instance which would be invalid after commit(). try ```swift secondvc.friend = Static.datastack.fetchExisting(friend) ```
Author
Owner

@ghost commented on GitHub (Dec 8, 2016):

worked like a charm, thank you very much !

@ghost commented on GitHub (Dec 8, 2016): worked like a charm, thank you very much !
Author
Owner

@JohnEstropia commented on GitHub (Dec 8, 2016):

Glad it worked :)

@JohnEstropia commented on GitHub (Dec 8, 2016): Glad it worked :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#106