0

在下面的代码中,self.theFiles打印null. 因为调用后[self.restClient loadMetadata:@"/"]getFiles方法继续工作。

我希望getFiles方法等到[self.restClient loadMetadata:@"/"]完成获取文件列表并将数据放入self.theFiles对象。但之后[self.restClient loadMetadata:@"/"]getFiles继续工作。

@synthesize theFiles;

- (void)getFiles:(CDVInvokedUrlCommand*)command
{
    if (![[DBSession sharedSession] isLinked]) {
        [[DBSession sharedSession] linkFromController:self];
    }
    else
    {
        [self.restClient loadMetadata:@"/"];
    }
    NSLog(self.theFiles); //Prints null
    NSLog(@"Finished");
}

#pragma mark DBRestClientDelegate methods

- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {

    NSArray* validExtensions = [NSArray arrayWithObjects:@"txt", @"text", nil];
    NSMutableArray* newPhotoPaths = [NSMutableArray new];
    for (DBMetadata* child in metadata.contents) {
        self.theFiles=child.path;
        NSString* extension = [[child.path pathExtension] lowercaseString];
        if (!child.isDirectory && [validExtensions indexOfObject:extension] != NSNotFound) {
            [newPhotoPaths addObject:child.path];
        }
    }

    NSLog(self.theFiles); //Prints the file list
}

输出:

null
Finished
/file1.txt
/file2.txt
..... the file list in my dropbox

是否可以将我的 getFiles 方法与委托同步工作?

更新

我更新了我的代码。我想锁定线程使用NSLock但不起作用。同样的结果。

@synthesize theFiles;

- (void)getFiles:(CDVInvokedUrlCommand*)command
{
    if (![[DBSession sharedSession] isLinked]) {
        [[DBSession sharedSession] linkFromController:self];
    }
    else
    {
       if ([self.theLock tryLock]) {
        [self.restClient loadMetadata:@"/"];
        NSLog(self.theFiles); //Prints null
        NSLog(@"Finished");
       }
    }

}

#pragma mark DBRestClientDelegate methods

- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {

    NSArray* validExtensions = [NSArray arrayWithObjects:@"txt", @"text", nil];
    NSMutableArray* newPhotoPaths = [NSMutableArray new];
    for (DBMetadata* child in metadata.contents) {
        self.theFiles=child.path;
        NSString* extension = [[child.path pathExtension] lowercaseString];
        if (!child.isDirectory && [validExtensions indexOfObject:extension] != NSNotFound) {
            [newPhotoPaths addObject:child.path];
        }
    }

    NSLog(self.theFiles); //Prints the file list
    [self.theLock unlock];
}
4

0 回答 0