user470763
user470763

Reputation:

Core Data: Is it safer to pass around objects or object ID's?

I'm trying to work out which is safer/best practice. I have a method that takes an object, gets some data, and saves it to that object. Should I pass that method the object or just the object ID and then re-fetch the object in the method? Is one better than the other or does it not make a difference?

e.g.

- (void)getNameFromWebForPerson:(Person *)p {
    //start nsoperation
    operationComplete(NSString *name){
        p.name = name
        [p save];
    }
}

or

- (void)getNameFromWebForPerson:(ObjectID *)oid {
    //start nsoperation
    operationComplete(NSString *name){
        Person *p = [fetchObjectForID:oid];
        p.name = name
        [p save];
    }
}

Upvotes: 2

Views: 122

Answers (1)

atreat
atreat

Reputation: 4413

It all depends on which thread you are running in the save method. The NSManagedObjectContext instance that you create to manage these objects should always be accessed from the same thread/queue. It is recommended that if you need/want to use multiple queues to manage objects that you pass the ObjectID around and query like you do in your second example.

See NSManagedObject's section on Concurrency

Upvotes: 3

Related Questions