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

Make awakeFromInsert useful again #174

Closed
jamesmoschou opened this issue Nov 15, 2013 · 5 comments
Closed

Make awakeFromInsert useful again #174

jamesmoschou opened this issue Nov 15, 2013 · 5 comments
Assignees
Milestone

Comments

@jamesmoschou
Copy link
Contributor

The awakeFromInsert method is typically used to initialize managed objects with default "baseline" values the first time they are inserted into any managed object context. For Simperium objects, it can no longer be assumed that this method will be invoked only once in the object's lifetime, since the object may exist across multiple clients.

I'm wondering if it is possible to add a method to SPManagedObject in the same vein as -[UIViewController isMovingFromParentViewController] that can be queried from within awakeFromInsert to determine if it is actually being created for the first time, or just synced from another device.

Something like:

@interface SPManagedObject : NSManagedObject

/** Returns a Boolean value that indicates that the object is in the process of being downloaded from a remote store.

This method returns YES only when called from inside the awakeFromInsert method.

@return YES if the object is being inserted because it was created remotely and downloaded to this device during a synchronization operation, otherwise NO.
*/
- (BOOL)isSynchronizingFromRemoteStore;

@end


@implementation SPManagedObject

- (BOOL)isSynchronizingFromRemoteStore
{
    return [[self.managedObjectContext.userInfo allKeys] containsObject:@"spSynchronizationContext"]
}

@end

Alternatively Simperium could just provide another hook such as -[SPManagedObject awakeFromLocalInsert].

@mackross
Copy link
Contributor

I think -[SPManagedObject awakeFromLocalInsert] is cleaner and could be paired with -[SPManagedObject awakeFromRemoteInsert]. An easy way to implement this could be to check the context of the managed object on -[SPManagedObject awakeFromInsert]. If it's a sibling work context it's a remote insert. If its part of the main context, it must be a local change.

@ghost ghost assigned jleandroperez Jan 9, 2014
@jleandroperez
Copy link
Contributor

@jamesmoschou @mackross hey there, hope you are doing great!.

I've been checking this ticket, and there's a catch to it. 'awakeFromInsert' is called when a NSManagedObject is first inserted into a NSManagedObjectContext. This means that a MO will have its 'awakeFromInsert' method called, at least, twice:

  • First Call: when inserted in a background thread, due to a remote insertion.
  • Second Call: when the worker MOC is saved, and the MO is 'moved' to the writer MOC.

Without using another structure to hold metadata (which i'd love to avoid!), this scenario translates directly into 'awakeFromRemoteInsert' being called first, and 'awakeFromLocalInsert' afterwards, for a remote insertion.

Assuming that the code you add to those methods (in your SPManagedObject subclass) takes that into consideration, it shouldn't be a problem.

Just wanted to ask you: would this represent a problem for your use case?

Thanks in advance!!

@mackross
Copy link
Contributor

It should call awakeFromLocalInsert only the second call: when the worker MOC is saved, and the MO is 'moved' to the writer MOC. I think before then the object is really still in Simperium's hands.

jleandroperez added a commit that referenced this issue Jan 13, 2014
@jleandroperez
Copy link
Contributor

@mackross implemented!. Thanks for your feedback!

@jamesmoschou
Copy link
Contributor Author

yay

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

No branches or pull requests

3 participants