gpt4 book ai didi

objective-c - Open WithCompletionHandler : is complete 后如何返回结果

转载 作者:行者123 更新时间:2023-11-29 11:12:38 25 4
gpt4 key购买 nike

想在Coredata数据库中查询一张照片

这是我的代码

这是 NSObjectSubclass 类别

//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]>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

View Controller 中的代码

//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];
}
}

将它们连接起来的 NSObject 类。

 //  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

coredata只有一个名为“Photo”的Entity,它只有一个属性“name”。问题是返回总是在 block 完成之前执行并且总是返回 NO。测试码here

或者我应该在查询时做一些除了 openWithCompletionHandler 以外的事情吗?

最佳答案

您需要修改您的方法以异步工作,例如 -openWithCompletionHandler:。它需要采用一个 block ,该 block 在已知答案时调用,并接收答案(真或假)作为参数。

然后,调用者应该传入一个 block ,该 block 执行已知答案后应该发生的任何事情。

或者,您应该延迟关心数据库中照片的整个逻辑 block 。它应该在打开完成后完成。

您必须显示更多代码才能获得更具体的建议。


因此,您可以将 isPhoto... 方法重写为:

+ (BOOL)checkIfPhoto:(NSString *)photoToCheck isInManagedDocument:(UIManagedDocument *)document handler:(void (^)(BOOL isHere))handler {
if (document.documentState == UIDocumentStateClosed) {
[document openWithCompletionHandler:^(BOOL success) {
handler([Photo isPhoto:photoToCheck here:document.managedObjectContext]);
}];
}
else
handler(NO);
}

然后你可以重做这个:

- (IBAction)queryFromDatabase:(UIButton *)sender {
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"test"];
UIManagedDocument *defaultDocument = [[UIManagedDocument alloc] initWithFileURL:url];
[checkPhoto checkIfPhoto:@"test" isInManagedDocument:defaultDocument handler:^(BOOL isHere){
if (isHere) {
[sender setTitle:@"Okay" forState:UIControlStateNormal];
} else {
[sender setTitle:@"NO" forState:UIControlStateNormal];
}
}];
}

关于objective-c - Open WithCompletionHandler : is complete 后如何返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10868720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com