magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.79k forks source link

Fixed issue when parmamendIDs was already obtained but NSManagedObject wasn't saved #1181

Closed m1entus closed 8 years ago

m1entus commented 8 years ago

There can be situation than permamentID was already obtained but context wasn't saved to persistent store so NSManagedObject will have still temporaryID.

  1. A in localContext --save--> A in mainContext (not saving to persistent store) --> obtainPermamentID
  2. localContext2 <--fetch A -- mainContext (not saving to persistent store)
  3. localContext2 --save--> mainContext
  4. MR_inContext:mainContext called on A from localContext

MR_inContext will return nil instead of NSManagedObject if he was already obtained .

tonyarnold commented 8 years ago

Thanks @m1entus — is there any chance you can write a test to verify this problem (and fix), so that it doesn't resurface again?

m1entus commented 8 years ago

Sure i will do that

m1entus commented 8 years ago
  1. My issue is that from parent contact object is not obtained permamentID.
  2. Why you are obtainingPermanentIDsBeforeSaving ?
  3. Could you expose MR_obtainPermanentIDsBeforeSaving ?
tonyarnold commented 8 years ago

My issue is that from parent contact object is not obtained permamentID.

I understand, and the test you've written makes that a bit clearer.

Why you are obtainingPermanentIDsBeforeSaving?

In the circumstance of the method you've updated (-[NSManagedObject MR_inContext:]), I believe (it has been a while) temporary NSManagedObjectIDs do not resolve between contexts.

Given the purpose of MR_inContext: (to retrieve a managed object from another context), it seemed wise to obtain the permanent IDs before attempting the lookup.

Could you expose MR_obtainPermanentIDsBeforeSaving?

Yeah, I can — but I'm not sure what it gives you that you can't build yourself using -[NSManagedObjectContext obtainPermanentIDsForObjects:error]. What are you intending to use it for?

m1entus commented 8 years ago

Current code is fine that's why i closed my PR, i had some issues with my code and temporaryIDs between contexts.

Yeah, I can — but I'm not sure what it gives you that you can't build yourself using -[NSManagedObjectContext obtainPermanentIDsForObjects:error]. What are you intending to use it for?

Same issue as here: https://github.com/magicalpanda/MagicalRecord/issues/1084 . I wanted to make main queue context with parent context. Instead of this i had to make: + (instancetype)re_newMainQueueContext { NSManagedObjectContext *context = [NSManagedObjectContext MR_newMainQueueContext]; context.parentContext = [NSManagedObjectContext MR_defaultContext]; [context re_obtainPermanentIDsBeforeSaving]; return context; }