난 내 응용 프로그램에서 iCloud에 UIDocument
을 활성화라고하고, 내가 클라우드에 문서를 저장, 문서 내가이 일을하고있어 포장의 아이 클라우드 문서의 업데이트 알림을받을 수 있습니다 :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
을 당신은 내가 맥을 여기서 볼 수 있듯이 hange를 선택하면 스폰 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