UIDocument永远不会调用dealloc

我有一个问题,我似乎无法dealloc UIDocument(用于iCloud)

运行NSMetaDataQuery后查找文档如下..

NSMetadataQuery *query = [[NSMetadataQuery alloc] init]; _query = query; [query setSearchScopes:[NSArray arrayWithObject: NSMetadataQueryUbiquitousDocumentsScope]]; NSPredicate *pred = [NSPredicate predicateWithFormat: @"%K == %@", NSMetadataItemFSNameKey, kFILENAME]; [query setPredicate:pred]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query]; [query startQuery]; 

我处理我的查询

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

然后加载或创建一个新文档。

 - (void)loadData:(NSMetadataQuery *)query { if ([query resultCount] == 1) { NSMetadataItem *item = [query resultAtIndex:0]; NSURL *url = [item valueForAttribute:NSMetadataItemURLKey]; MyDocument *doc = [[[MyDocument alloc] initWithFileURL:url] autorelease]; [doc openWithCompletionHandler:^(BOOL success) { if (success) { NSLog(@"iCloud document opened %@", doc); [doc updateChangeCount:UIDocumentChangeDone]; } else { NSLog(@"failed opening document from iCloud"); } }]; } else { NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent: @"Documents"] URLByAppendingPathComponent:kFILENAME]; MyDocument *doc = [[[MyDocument alloc] initWithFileURL:ubiquitousPackage] autorelease]; [doc saveToURL:[doc fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) { if (success) { [doc openWithCompletionHandler:^(BOOL success) { NSLog(@"new document opened from iCloud"); [doc updateChangeCount:UIDocumentChangeDone]; }]; } }]; } } 

NSLog(@"iCloud document opened %@", doc); 显示每个UIDocument的不同内存地址。

我的UIDocument子类中有一个NSLog,它永远不会被调用。 我无法看到它被保留在哪里,我没有发布它。 每当我想要同步我的云数据时,就会运行此查询,这种情况经常发生。 数据正确同步。

我遇到了奇怪的崩溃,我的应用程序只是靠近仪表板,调试中没有任何东西(从以前的经验我知道这经常是应用程序消耗太多内存并被终止。)

我认为我的UIDocument正在泄漏,在这个假设中我是否正确,这是我第一次与iCloud搏斗,所以我仍然在一些事情的黑暗中。

我的子类具有以下属性:

 @property (copy, nonatomic) NSData *infoData; @property (copy, nonatomic) NSMutableArray *firstArray; @property (copy, nonatomic) NSMutableArray *secondArray; @property (copy, nonatomic) NSMutableArray *thirdArray; 

我没有使用ARC。

我没有意识到我必须这样做:

  [doc updateChangeCount:UIDocumentChangeDone]; [doc closeWithCompletionHandler:nil]; 

显然,如果一个文件是开放的,那么允许它被解除分配是不明智的!

卫生署! 希望这能在将来节省一些时间。