-->

UIDocument从未调用的dealloc(UIDocument never calling de

2019-09-19 09:09发布

我有一个问题,我似乎在哪里可以不要的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。

我有一个NSLog的在我的UIDocument子类,它永远不会被调用。 我无法看到它被保留,我不释放它在哪里。 此查询跑了,每当我想要同步我的云数据,这相当定期发生。 该数据能够正常同步。

我遇到奇怪的崩溃在我的应用程序将简单地关闭到仪表板,什么也没有在调试(从以前的经验,我知道这一点经常被应用程序花费太多的内存和被终止。)

我认为我的UIDocument漏水,我会在这个假设是正确的,这是我第一次与iCloud的搏斗所以我仍然在黑暗中过了一些东西。

我的子类具有以下特性:

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

我不使用ARC。

Answer 1:

我不知道,我不得不这样做:

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

显然,如果一个文件打开进行写入,那么这将不会是明智的,允许它被释放!

卫生署! 希望这样可以节省有人在未来的一段时间。



文章来源: UIDocument never calling dealloc