Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to return result after OpenWithCompletionHandler: is complete
    primarykey
    data
    text
    <p>Want to query a photo in the Coredata database</p> <p>this is my code</p> <p>this is the NSObjectSubclass category</p> <pre><code>//Photo+creak.h #import "Photo+creat.h" @implementation Photo (creat) +(Photo *)creatPhotoByString:(NSString *)photoName inManagedObjectContext:(NSManagedObjectContext *)context{ Photo *picture = nil; NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photo"]; request.predicate = [NSPredicate predicateWithFormat:@"name = %@", photoName]; NSArray *matches = [context executeFetchRequest:request error:nil]; if (!matches || [matches count]&gt;1) { //error } else if ([matches count] == 0) { picture = [NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext:context]; picture.name = photoName; } else { picture = [matches lastObject]; } return picture; } + (BOOL)isPhoto:(NSString *)photoName here:(NSManagedObjectContext *)context{ NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photo"]; request.predicate = [NSPredicate predicateWithFormat:@"name = %@", photoName]; NSArray *matches = [context executeFetchRequest:request error:nil]; switch ([matches count]) { case 1: return YES; break; default: return NO; break; } } @end </code></pre> <p>code inside of view controller</p> <pre><code>//View Controller - (IBAction)insertData:(UIButton *)sender { NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:@"test"]; UIManagedDocument *defaultDocument = [[UIManagedDocument alloc] initWithFileURL:url]; if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]]) { [defaultDocument saveToURL:defaultDocument.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:NULL]; } [defaultDocument openWithCompletionHandler:^(BOOL success) { [Photo creatPhotoByString:@"test" inManagedObjectContext:defaultDocument.managedObjectContext]; [defaultDocument saveToURL:defaultDocument.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:NULL]; }]; [sender setTitle:@"Okay" forState:UIControlStateNormal]; [sender setEnabled:NO]; } - (IBAction)queryFromDatabase:(UIButton *)sender { NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:@"test"]; UIManagedDocument *defaultDocument = [[UIManagedDocument alloc] initWithFileURL:url]; BOOL isItWorking = [checkPhoto isPhoto:@"test" inManagedDocument:defaultDocument]; if (isItWorking) { [sender setTitle:@"Okay" forState:UIControlStateNormal]; } else { [sender setTitle:@"NO" forState:UIControlStateNormal]; } } </code></pre> <p>The NSObject Class that hook them up. </p> <pre><code> // checkPhoto.m #import "checkPhoto.h" @implementation checkPhoto + (BOOL)isPhoto:(NSString *)photoToCheck inManagedDocument:(UIManagedDocument *)document{ __block BOOL isPhotoHere = NO; if (document.documentState == UIDocumentStateClosed) { [document openWithCompletionHandler:^(BOOL success) { isPhotoHere = [Photo isPhoto:photoToCheck here:document.managedObjectContext]; }]; } return isPhotoHere; } @end </code></pre> <p>The coredata only have on Entity named "Photo", and it got only one attribute "name". The problem is that the return always get execute before the block is complete and always return NO. Test code <a href="https://docs.google.com/open?id=0B519iRTC07VBUF84ZHVVSVFkcVk" rel="nofollow">here</a></p> <p><strong>Or should I do something else than openWithCompletionHandler when querying?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload