0
votes

In my viewWillAppear I execute multiple fetch requests. I noticed that on the first fetch request, nothing is found even though I know items should be found. I tried sleeping the main thread before executing the first fetch request and sure enough items were found. I'm only running on the simulator too btw.

So my question is, does Core Data take some time to "initialize" or anything? Will I have to worry about this when testing on a device (I don't have developer membership yet so I can't test it on a device)? Is there any way to "check" if it is safe to execute a fetch request?

I also checked the NSError for the first fetch request with no delay and it was null.

EDIT:

My viewWillAppear code was requested so here it is. Not very helpful though.

    [NSThread sleepForTimeInterval:0.1];
    for (NSString *category in self.categories) {
        [self updateSection:category];
    }

self.cateogires is just my sections of my table view and updateSection: updates the sections.

EDIT2:

I did some further investigating with NSLog statements and here's what I found.

I added NSLog statements to log how many objects are found after a fetch request and also the current state of the document. For some reason I'm getting fetch results even though the document appears to be closed.

Category1 has 3 values
Doc is open? NO
Category2 has 6 values
Doc is open? NO
Category3 has 3 values
Doc is open? NO
Category4 has 2 values
Doc is open? NO

I think my problem is that I am not using the completionHandler of [myManagedDocument openWithCompletionHandler: ] to wait and execute my fetch request. However, why then, do I get fet results if I am querying when the document is closed?

1
Can you post the code in from your viewWillAppear method?Alex
Maybe your managed object context or persistent store coordinator or whatever in your persistence stack has not been initialized and is nil? Calling the fetch request on a nil object would not get an error, it would just do nothing as per Objective-C policy for calls to nil.morningstar
Or maybe you have a persistent store coordinator but didn't add any persistent stores yet - though I think that gives an error, but not sure.morningstar
The moc is not nil for sure because only the first loop of the for loop gets a miss. All the other loops work fine and make different fetch request using the same moc. Also, when I sleep the thread as in my code, the first loop works fine; its only when I don't sleep that it fails.user1529956
see my edit2 for some new information on the problemuser1529956

1 Answers

0
votes

I suggest you try changing the concurrency type to

NSMainQueueConcurrencyType. This will make the operations sequential.