1

I have more of a conceptual question than code based because my code works.

When my app launches I fetch the sessionObject from coreData and validate the authToken.

This code works when in my loading controller. The Fetch request works and returns an array of sessionObjects. However in App Delegate where I validate the authToken the returned array is empty. Why does the code work in a controller but not in the App Delegate? There are no errors from the fetch request. The context is not nil. It's the exact code I use in the loading controller and that works.

Do I have to do requests differently for CoreData in the App Delegate? Can I use a fetch request in App Delegate?

Sample Code in the app Delegate DidBecomeActive method. I use DidBecomeActive so we can validate on return from background and init.

    // check for valid authtoken if present so the correct home screen will display
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"CurrentSession" inManagedObjectContext:[self managedObjectContext]];

[request setEntity:entity];
CurrentSession *sessionObj = nil;
NSError *cdError = nil;
if([self managedObjectContext] == nil){
    NSLog(@"context is nil");
}
NSArray *sessionArray = [[[self managedObjectContext] executeFetchRequest:request error:&cdError] mutableCopy];
if (sessionArray != nil && [sessionArray count]) {

    sessionObj = (CurrentSession *)[sessionArray lastObject];
            NSLog(@"Found session %@",sessionObj.authToken);
    if (![sessionObj.authToken isEqualToString:@""]) {
        [Solid_Utilities validateAuthToken:[self managedObjectContext]];
    }
} else {
    NSLog(@"NO SESSION FOUND");
}

EDIT I'm sure my issue may be thread related. In the loading controller I run a lot of tasks on separate threads and I assume App Delegate runs on main thread. However the context I provide to the loading controller is generated in app delegate.

EDIT I did a isMainThread check in App Delegate and Loading controller and both came back as true. Not sure why if they use the same context and store they wouldn't return the same array of objects.

4
  • Show appDelegate's managedObjectContext method. Do you use the AppDelegate's managedObjectContext method in your view controller as well, or does view controller have its own (different?) implementation to get the managedObjectContext? Commented Feb 12, 2013 at 19:33
  • As per Apple Guidelines I generate the context in the app delegate and then pass along from controller to controller. I use the stock Core Data Stack methods that Xcode gives you in app delegate when doing a core data app. If the context exists it returns that else generates a new one Commented Feb 12, 2013 at 19:49
  • If you're doing work on different threads, you need to either use one of the queue concurrency types for the MOC (e.g. NSMainQueueConcurrency) and then use one of the block-based MC calls-- or else use your own synchronization scheme. As for the app delegate, the code runs on whatever thread you call it on, there's nothing special about the class as it relates to threads. Commented Feb 12, 2013 at 21:47
  • Looks like they are both on the mainthread. If I move the code out of the app delegate DidBecomeActive into a controller with an NSNotificationCenter for didbecomeactive it works. I'm just trying to figure out why it hates the code being in the app delegate. Commented Feb 13, 2013 at 1:44

1 Answer 1

1

Looks like the answer was related to another issue I had. Turns out the original developer added the core data stack Into the base controller and I was using the stack in the app delegate and passing it to the base controller. The base controller would overwrite my context with its own stack and this is why I couldn't get the fetch results expected in app delegate.

I would say from what I've learned over this project is to create the context in the app delegate and pass it to your first controller. Then in prepareForSegue or when you manually push pass the context along. Also in the view will disappear you check if your going back and update the stores context. If you do any multi threading then make sure in app delegate your context is nsmainconcurrencytype so you can create child context for other threads. This will keep data results as expected and conflicts to minimal.

Thanks for all the input on this. Your responses helped me track down the stupid issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.