Sandeep
Sandeep

Reputation: 21154

Core data and holding over NSManagerObject

I dont have a clear understanding over how core data really works. May be this is the result that I am facing. I have an application in which I use a single managedObjectContext through the application. I know that managedObjectContext holds over all the managedObjects in it and until I reset the context, the object is always retained by the context itself. So, what I do is I fetch some main object like User data and then keep on passing the same object throughout the view controller. If some changes occur then the object is refreshed by the some other code. This works pretty well in almost all cases.

But, I do not keep strong pointer to the object in any viewcontrollers. I just keep a weak reference over it, since the object is itself retained by the managedObjectContext pool. But, I some times have problem with this strategy, when the object is refreshed by syncing to remote. My weak pointer becomes useless. It has no data in it. All the attributes becomes empty. Although it shows all the attributes name when I log, the values are just nil. How do I make sure that my object is always refreshes and is sync with the main context ? Is this way of doing keeping weak pointer to managedObject a bad idea. I would say I dont retain it in any viewcontroller. All the view controllers just use weak pointer.

My code would look like this,

In the root view controller I fetch the data,

-(void)tableView:(UITableView*)tv didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
  User *user = [self.fetchedResultsController objectAtIndexPath:indexPath];
  FirstViewController *firstViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"firstViewController"];
  firstViewController.user = user;
}

FirstViewController:

@interface FirstViewController:UIViewController
  @property(nonatomic, weak) User *user
@end

I do many things with the object here and in the mean time, I may update the object from the rootViewController. But, since it is the same object it appears to be updated most of the time.

Then again in first view controller;

-(void)tableView:(UITableView*)tv didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
  User *user = [self.fetchedResultsController objectAtIndexPath:indexPath];
  SecondViewController *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
}

SecondViewController:

@interface SecondViewController:UIViewController
  @property(nonatomic, weak) User *user
@end

Here again, the object is referenced weakly. I do many things with the object or some other view controllers may sync from remote but it keeps on updating. This object is passed to the deep hierarchy until the last view controller, and all of those view controller keep weak pointers to it.

Most of the time it is quite fine. But, many times in my current view controller even if the object is synced to network, it appears to be null, I mean the object is still there with all the property descriptions but has not values in it. But, then appears suddenly after some time.

What might have happened. How do I keep the object synced to the root context and refresh when any changes occur ? Keeping a strong pointer, would this be able to solve all my issues. I guess it might be because of the object not being faulted. How can I exclusively fault an object in one of the object so that it becomes visible to all other objects.

Really would appreciate your help and consideration. I may have a bad english but if some one understands my question and could express it in better way, I would really be happy for that too. :)

Upvotes: 2

Views: 146

Answers (1)

matt
matt

Reputation: 535566

If you need the object to survive a refresh, don't hold it weakly, hold it strongly. Then you can call refreshObject: on it after the context is refreshed. Alternatively, use lazy fetching in your accessor so that when you access your object and it is nil, we do the fetch again.

See also:

http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/Articles/cdTroubleshooting.html

Upvotes: 1

Related Questions