2011-11-25 51 views
3

我有這樣的代碼(我嘗試打開從雲文檔):openWithCompletionHandler完成處理參數始終= NO

NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K ENDSWITH '.card'", NSMetadataItemFSNameKey]; 

NSMetadataQuery *query = [[NSMetadataQuery alloc] init]; 
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]]; 
[query setPredicate:pred]; 

[[NSNotificationCenter defaultCenter] 
addObserver:self 
selector:@selector(queryDidFinishGathering:) 
name:NSMetadataQueryDidFinishGatheringNotification 
object:query]; 

[[NSNotificationCenter defaultCenter] 
addObserver:self 
selector:@selector(queryDidStartGathering:) 
name:NSMetadataQueryDidStartGatheringNotification 
object:query]; 

[[NSNotificationCenter defaultCenter] 
addObserver:self 
selector:@selector(queryDidUpdate:) 
name:NSMetadataQueryDidUpdateNotification 
object:query]; 

[query startQuery]; 

// ================= ======== openWithCompletionHandler完成塊的

- (void)queryDidFinishGathering:(NSNotification *)notification { 

    NSMetadataQuery *query = [notification object]; 
    [query disableUpdates]; 
    [query stopQuery]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:NSMetadataQueryDidFinishGatheringNotification 
                object:query]; 

    for (NSMetadataItem* item in [query results]) { 
     NSURL *url = [item valueForAttribute:NSMetadataItemURLKey]; 
     BCCardDocument *doc = [[[BCCardDocument alloc] initWithFileURL:url] autorelease]; 
     [doc openWithCompletionHandler:^(BOOL success) { 
      if (success) { 
       NSLog(@"%@", doc.card.number); 
      } 
     }]; 

    } 

} 

成功參數始終等於NO。這可能是什麼原因?

回答

1

我無法確切地告訴你你需要做什麼,但是我可以告訴你如何得到錯誤信息,以便你能弄明白。

在你BCCardDocument類的@implementation部分,添加這樣的事情:

- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted { 
    NSLog(@"Error: %@ userInfo=%@", error.localizedDescription, error.userInfo); 
    [super handleError:error userInteractionPermitted:userInteractionPermitted]; 
} 
0

我有非常類似的代碼,它工作正常。我假設你的BCCardDocument是UIDocument的子類?如果是這樣,它需要有這兩種方法:

- (id)contentsForType:(NSString *)typeName error:(NSError **)outError 

- (BOOL)loadFromContents:(id)contents 
       ofType:(NSString *)typeName 
       error:(NSError *__autoreleasing *)outError { 

唯一的區別是,我不叫stopQuery。