Frage

I have such code(I try to open documents from cloud):

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];

// =========================

- (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);
            }
        }];

    }

}

But success argument of openWithCompletionHandler completion block always equals to NO. What can be reason of it?

War es hilfreich?

Lösung

I can't tell you exactly what you need to do, but I can tell you how to get at the error message so you can figure it out.

In your BCCardDocument class's @implementation section, add something like this:

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

Andere Tipps

I have very similar code to this and it works fine. I assume that your BCCardDocument is a subclass of UIDocument? If so, it needs to have these two methods:

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

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

The only other difference is that I don't call stopQuery.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top