I am making my first IOS application and using JSON and core data for getting and storing my data.
But for some reason or another it won't open it's NSDocument
. This is what I am doing.
- (void)useDocument
{
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.genkDatabase.fileURL path]]) {
// does not exist on disk, so create it
[self.genkDatabase saveToURL:self.genkDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
NSLog(@"test1");
[self setupFetchedResultsController];
NSLog(@"test12");
[self fetchFlickrDataIntoDocument:self.genkDatabase];
}];
} else if (self.genkDatabase.documentState == UIDocumentStateClosed) {
NSLog(@"test2");
// exists on disk, but we need to open it
[self.genkDatabase openWithCompletionHandler:^(BOOL success) {
NSLog(@"test4");
[self setupFetchedResultsController];
NSLog(@"test5");
}];
} else if (self.genkDatabase.documentState == UIDocumentStateNormal) {
NSLog(@"test3");
// already open and ready to use
[self setupFetchedResultsController];
}
}
- (void)setupFetchedResultsController // attaches an NSFetchRequest to this UITableViewController
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"News"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"News.title" ascending:YES]];
// no predicate because we want ALL the Photographers
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.genkDatabase.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
NSLog(@"test10");
}
When you look at my log, you see the following.
Test 2 Test 4 Test 10 Test 5
As you can see it opens it succefully. Then executes the method setupFetchedResultsController
. But then it suddenly stops. I don't get any errors.
What It normally should do is go to the fetchFlickrDataIntoDocument
method.
Can anybody help me please ?
I believe this method [self fetchFlickrDataIntoDocument:self.genkDatabase];
should be called at the time your DB doesn't exist and so you want to create it on your disk within a document directory. When you are running your test your DB exist already and has just not been opened so your program try to open it. You basically just follow the conditional statement : if (self.genkDatabase.documentState == UIDocumentStateClosed)
. I don't see why your program should call the fetchFlickrDataIntoDocument:self
since it should only happen when your DB doesn't exist.