views:

44

answers:

1

Hi,

I'm relatively new to iPhone dev and have been trying to build an application to understand how CoreData works.

I'm trying to implement some functionality that will allow me to detect if anything has been persisted to store and if so - give the user the option to delete those objects.

I wondered if there are timing issues that need to be taken into consideration when retrieving data on device as opposed to simulator.

I created a simple core data app using the XCode wizard to present some data using a UITableViewController class.

I have the following code snippet from my RootViewController class:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSSet* obj = nil;
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];    
    obj = [context registeredObjects]; 

    if (obj.count == 0)
    {
        // Do stuff to add initial data to store 
    }
    else
    {
        // Do stuff to delete objects from store if user wants
    }
}

If I run this code on the simulator (having previously added to my store) I get a non-zero value for obj.count, but if I run it on device, obj.count is always zero even if I've previously added to the store.

Any ideas where I might be going wrong?

Perhaps my overall approach for what I'm trying to achieve is incorrect......

A: 

As a follow up, maybe the question I should be asking is if I do this (again within my RootViewController) :

- (NSFetchedResultsController *)fetchedResultsController {

<snippet_begin>

NSFetchedResultsController *aFetchedResultsController = 
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

NSSet* obj = nil;
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];    
obj = [context registeredObjects];

</snippet_end>

}

Is the time at which the context becomes valid deterministic ?

jaib