NSMetadataQuery无法完成收集(无通知) [英] NSMetadataQuery doesn't finish gathering (no notification)

查看:230
本文介绍了NSMetadataQuery无法完成收集(无通知)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序制作备份管理器(通过iCloud)。我做了一些测试,基础工作。但几天后它停了下来。我正在使用 NSMetadataQuery 来搜索备份文件是否存在。我的备份文件被命名为例如 Backup29112011154133.xml 其中数字代表备份日期(格式为 ddMMyyyyHHmmss )。我在 -viewDidAppear 中检查它:

   - (void)viewDidAppear :(BOOL)动画{
[self checkForRemoteFile];
}

- (void)checkForRemoteFile {
NSURL * ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if(ubiq){
NSMetadataQuery * query = [[NSMetadataQuery alloc] init];
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
NSPredicate * pred = [NSPredicate predicateWithFormat:@%K like'Backup *',NSMetadataItemFSNameKey];
[query setPredicate:pred];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering :) name:NSMetadataQueryDidFinishGatheringNotification object:query];
[query startQuery];
} else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@iCloud暂时不可用消息:nil delegate:self cancelButtonTitle:@CloseotherButtonTitles:nil];
[alert setTag:TAG_ALERT_NOICLOUD];
[alert show];
}
}

- (void)queryDidFinishGathering:(NSNotification *)notif {
NSMetadataQuery * query = [notif object];
[query disableUpdates];
[query stopQuery];
[self loadRemoteFile:query];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];
}

- (void)loadRemoteFile:(NSMetadataQuery *)query {
if([query resultCount] == 1){
canRestore = YES;
NSMetadataItem * item = [query resultAtIndex:0];
//解析备份文件
[self.tableView reloadData];
} else {
canRestore = NO;
modifDate = @never;
backupInfoLoaded = YES;
[self.tableView reloadData];
}
}

问题在于 - (void)queryDidFinishGathering :( NSNotification *)notif 永远不会被执行。我把breakpints和NSLogs放在那里,但没有任何事情发生。



我还试图检查其他通知,例如'查询确实开始收集'和'查询过程'。仅发布'query did start'通知。



我还附带了iCloud注册和权利文件的AppID。



<你能帮我解决一下发生的事吗?也许我错过了什么?

解决方案

通过为<$创建 ivar 进行修复c $ c> NSMetadataQuery 。



我不知道为什么没有NSMetadataquery ivar,应用程序无法读取数据。


I'm making a backup managrer for my App (via iCloud). I did some tests and the basics worked. But few days later it stopped. I'm using NSMetadataQuery for searching if backup file exists. My backup files are named e.g. Backup29112011154133.xml where numbers represent date of the backup (formatted as ddMMyyyyHHmmss). I check for it in -viewDidAppear:

- (void)viewDidAppear:(BOOL)animated {
    [self checkForRemoteFile];
}

- (void)checkForRemoteFile {
    NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    if (ubiq) {
        NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
        [query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K like 'Backup*'",NSMetadataItemFSNameKey];
        [query setPredicate:pred];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query];
        [query startQuery];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iCloud is unavailable at the moment" message:nil delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert setTag:TAG_ALERT_NOICLOUD];
        [alert show];
    }
}

- (void)queryDidFinishGathering:(NSNotification *)notif {
    NSMetadataQuery *query = [notif object];
    [query disableUpdates];
    [query stopQuery];
    [self loadRemoteFile:query];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];
}

- (void)loadRemoteFile:(NSMetadataQuery *)query {
    if ([query resultCount] == 1) {
        canRestore = YES;
        NSMetadataItem *item = [query resultAtIndex:0];
        // parse the backup file
        [self.tableView reloadData];
    } else {
        canRestore = NO;
        modifDate = @"never";
        backupInfoLoaded = YES;
        [self.tableView reloadData];
    }
}

The problem is that - (void)queryDidFinishGathering:(NSNotification *)notif is never executed. I put breakpints and NSLogs ion there but nothing happend.

I also tried to check for other notifications e.g. 'query did start gathering' and 'query process'. Only 'query did start' notification is posted.

I also have AppID with iCloud registered and entitlements file attached.

Can you help me out what's going on? Maybe I missed something?

解决方案

FIXED by creating ivar for NSMetadataQuery.

I don't know why the application can't read data without NSMetadataquery ivar.

这篇关于NSMetadataQuery无法完成收集(无通知)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆