我啓用的iCloud UIDocument
在我的應用程序,我存儲在雲上的文檔,該文檔打包,對收到的iCloud文檔的更新的通知我這樣做:NSMetadataQueryDidUpdateNotification叫來幾個時間
- (void)startQuery {
[self stopQuery];
NSLog(@"Starting to watch iCloud dir...");
_query = [self documentQuery];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(processiCloudFiles:)
name:NSMetadataQueryDidFinishGatheringNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(processiCloudFiles:)
name:NSMetadataQueryDidUpdateNotification
object:nil];
[_query startQuery];
}
- (void)stopQuery {
if (_query) {
NSLog(@"No longer watching iCloud dir...");
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidUpdateNotification object:nil];
[_query stopQuery];
_query = nil;
}
}
- (NSMetadataQuery *)documentQuery {
NSMetadataQuery * query = [[NSMetadataQuery alloc] init];
if (query) {
[query setSearchScopes:[NSArray arrayWithObject:
NSMetadataQueryUbiquitousDocumentsScope]];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K == %@",
NSMetadataItemFSNameKey,
kFILENAME];
[query setPredicate:pred];
}
return query;
}
- (void)processiCloudFiles:(NSNotification *)notification {
[_query disableUpdates];
if ([_query resultCount] == 1) {
NSMetadataItem *item = [_query resultAtIndex:0];
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
if (!self.folder) {
self.folder = [[MyDocument alloc] initWithFileURL:url];
}
[self.folder openWithCompletionHandler:^ (BOOL success) {
if (success) {
NSLog(@"loadData - doc opened from cloud %i", self.folder.count);
[self.folder closeWithCompletionHandler:^(BOOL success) {
NSLog(@"doc closed");
}
];
} else {
NSLog(@"failed to open");
} }];
}
}
啓動所有我打電話[self startQeury]
,問題是NSMetadataQueryDidUpdateNotification
稱爲通知幾個時間,如果我做的文檔中的變化,你可以怎麼看日誌:
2013-10-12 01:43:54.933 Starting to watch iCloud dir...
2013-10-12 01:43:56.119 loadData - doc opened from cloud 56
2013-10-12 01:43:56.120 doc closed
2013-10-12 01:44:01.552 item deleted
,你可以在這裏看到我做交流在這裏下面你可以看到產卵4通知:
2013-10-12 01:44:08.110 loadData - doc opened from cloud 55
2013-10-12 01:44:08.111 doc closed
2013-10-12 01:44:11.942 loadData - doc opened from cloud 55
2013-10-12 01:44:11.943 doc closed
2013-10-12 01:44:13.198 loadData - doc opened from cloud 55
2013-10-12 01:44:13.199 doc closed
2013-10-12 01:44:14.925 loadData - doc opened from cloud 55
2013-10-12 01:44:14.926 doc closed
爲什麼叫幾次?在我的應用程序中,當更新完成時我需要處理更改,所以我只需要一個通知...我該怎麼辦?
編輯: 我有什麼嘗試@rmaddy在回答提示,我有嘗試這個辦法:
if ([[item valueForAttribute:NSMetadataUbiquitousItemIsDownloadedKey] boolValue]) {
if (!self.folder) {
self.tvfilesFolder = [[MyDocument alloc] initWithFileURL:url];
}
[self.folder openWithCompletionHandler:^ (BOOL success) {
if (success) {
NSLog(@"loadData - doc opened from cloud %i", self.folder.count);
[self.folder closeWithCompletionHandler:^(BOOL success) {
NSLog(@"doc closed");
}
];
} else {
NSLog(@"failed to open");
} }];
}
但同樣我的問題是這樣的日誌:
2013-10-12 10:35:26.084 Starting to watch iCloud dir...
2013-10-12 10:35:30.306 loadData - doc opened from cloud 54
2013-10-12 10:35:30.307 doc closed
那麼我編輯了一個文件,這是日誌:
2013-10-12 10:35:53.233 loadData - doc opened from cloud 54
2013-10-12 10:35:53.235 doc closed
2013-10-12 10:35:56.703 loadData - doc opened from cloud 54
2013-10-12 10:35:56.704 doc closed
2013-10-12 10:35:58.489 loadData - doc opened from cloud 54
2013-10-12 10:35:58.490 doc closed
我該怎麼辦?
編輯2:
我已經嘗試用這樣的:
if ([[item valueForAttribute:NSMetadataUbiquitousItemIsDownloadedKey] boolValue] && [[item valueForAttribute:NSMetadataUbiquitousItemIsUploadedKey] boolValue]) {
....
}
,似乎工作,你在想什麼?我可以處理所有的時間有變化,或者我可以以這種方式錯過一些通知?
doens't工作,我試着用這個鍵:NSMetadataUbiquitousItemIsDownloadedKey,看問題,我已經與日誌編輯它,似乎沒有任何變化... – Piero
我已經編輯了再次問題... – Piero