2012-07-12 38 views
3

我有一個問題,我似乎在哪裏可以不要的dealloc UIDocument(iCloud中使用)UIDocument從未調用的dealloc

運行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。

回答

5

我不知道,我不得不這樣做:

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

顯然,如果一個文件打開進行寫入,那麼這將不會是明智的,允許它被釋放!

Doh!希望將來可以節省一些時間。

+0

以下是上述優秀答案的快速版本:document?.updateChangeCount(UIDocumentChangeKind.Done) document?.closeWithCompletionHandler(nil) – Garret 2015-04-12 19:31:01