【问题标题】:UIDocument never calling deallocUIDocument 从不调用 dealloc
【发布时间】:2012-07-12 01:36:51
【问题描述】:

我有一个问题,我似乎无法解除 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。

【问题讨论】:

    标签: ios icloud retain dealloc uidocument


    【解决方案1】:

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

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

    显然,如果一个文件已打开以供写入,那么允许它被释放是不明智的!

    哇!希望这可以为将来节省一些时间。

    【讨论】:

    • 这是上述出色答案的快速版本:document?.updateChangeCount(UIDocumentChangeKind.Done) document?.closeWithCompletionHandler(nil)
    • 我也面临同样的问题。即使我没有打开文档并且文档状态已关闭,是否需要调用关闭,只需使用 url 初始化它?
    猜你喜欢
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 2012-08-08
    • 2013-10-19
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多