0

In my app, I use Core Data foundation to store my data (some images). I need to fetch images from my data base and display them in a table view. When my app is loading, it will fetch some images from database. However, I am quite confused that sometimes the method

[context executeFetchRequest: request error: &error] 

would fail to fetch data from my database while sometimes it works well (I am quite sure the data is indeed in the database). I add some test code here:

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"Album" inManagedObjectContext:context]];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"number" ascending:YES];
    request.sortDescriptors = @[sortDescriptor];    
    NSArray *matches = [context executeFetchRequest:request error:&error];

    for (int i = 0; i < 10; i++) {
        if ([matches count]) {
            break;
        }
        matches = [context executeFetchRequest:request error:&error];
        NSLog(@"try one more fetch for my data");
    } 

If the database is empty, the loop will exist after trying 10 times. But when my database is not empty, from NSLog method, I find that sometimes it fetched data successfully after trying 2 times and sometimes after 5 times, and sometimes after trying 10 times it still failed to fetch data. To be more specific, this phenomenon only happens when the first few [context executeFetchRequest: request error: &error] get executed. After that, the method works well.

So I want to know why does this happen, can anyone help me?

Jay
  • 998
  • 2
  • 15
  • 24
  • was there an error (add to your log line)? are you starting with an empty store? do you populate your store in the background? – Dan Shelly May 07 '14 at 14:17
  • No error. And I am quite sure my database is not empty, otherwise it will not fetch photos and display them well after trying 2 or 3 times. – Jay May 07 '14 at 14:22
  • Is `context` an asynchronously-filled instance variable? It shouldn't be. What happens if you actually log `error` instead of just promising us that there isn't one? – Tommy May 07 '14 at 14:25

1 Answers1

2

Your question is very generic. You should add details (what do you mean with fail, share your model, share the fetch request, etc)

Few tips here.

First of all executeFetchRequest:error: takes an error as param. So you have check it for errors.

NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
    // log error here
} else {
    // log results here
}

Second, if you are using a SQLite persistent store you can see logs Core Data creates in the following way: XCode4 and Core Data: How to enable SQL Debugging.

Finally, if you are using a SQLite persistent you can also inspect saved data. A possible path could be:

/Users/NAME/Library/Application Support/iPhone Simulator/VERSION/Applications/APP ID/Documents/DB FILE.sqlite

In addition to Dan Shelly's comment, are you performing any background operation to display images?

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • If it fetched photos successfully, some photos would be displayed in my table view. And when it failed, nothing would be displayed in my tableview. I don't take any background operation to display photos. In my table view, I write some code that if some photos(photos that are the covers of my albums) were fetched, then display them and if no photos fetches, display nothing. – Jay May 07 '14 at 14:46
  • What makes me crazy is that with the same code executed, sometimes it fetches photos I need instantly, while sometimes it fetched them successfully after the fetch method executed a couple of times. And what's worse, sometimes it failed to fetch after the fetch method executed 10 times. – Jay May 07 '14 at 14:49