gpt4 book ai didi

ios - UIDocument 从不调用 dealloc

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:39 26 4
gpt4 key购买 nike

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

很明显,如果一个文件是为写入而打开的,那么允许它被释放是不明智的!

呸!希望这可以在将来为某人节省一些时间。

关于ios - UIDocument 从不调用 dealloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11449530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com