无限循环遍历云存储文件结构[Box api]

时间:2014-02-13 07:17:23

标签: ios objective-c recursion box-api

我试图从盒子api中获取元数据,因此我需要遍历整个目录结构。这是代码:

- (IBAction)ls {

    // INITIALIZE folderID  (will be nil only for the root folder)

    if (self.folderID == nil)
    {
        self.folderID = BoxAPIFolderIDRoot;
        self.folderName = @"Root";   
    }

    // START TRAVERSING DIRECTORY TREE
    [self traverseItemsWithFolderID:self.folderID name:self.folderName]; 
    }


- (void)traverseItemsWithFolderID:(NSString *)folder name:(NSString *)namefolder

{   
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [self fetchFolderItemsWithFolderID:folder name:namefolder]; //   <- List Files/dirs in directory namefolder

        dispatch_async(dispatch_get_main_queue(), ^{

   // Create box object "item" to traverse folderItemsArray
    BoxItem * item = (BoxItem *)[self.folderItemsArray objectEnumerator];
    for (item in self.folderItemsArray)
    {
        if([item.type isEqualToString:BoxAPIItemTypeFolder])
            {
                NSLog(@"SubFolder id %@", item.modelID);
                NSLog(@"SubFolder name %@", item.name);
                [self traverseItemsWithFolderID:item.modelID name:item.name];

            }
    }

    });
   });
}

and this last method:

- (void)fetchFolderItemsWithFolderID:(NSString *)innerFolder name:(NSString *)innerFolderName
{

    BoxFoldersResourceManager *mgr = [BoxSDK sharedSDK].foldersManager;
    BoxFoldersRequestBuilder *bldr = [[BoxFoldersRequestBuilder alloc] initWithQueryStringParameters:@{ @"fields" : @"name,type,id,etag,size,modified_at,hash" }];

    [mgr folderItemsWithID:innerFolder requestBuilder:bldr success: ^(BoxCollection *collection)
    {
        self.folderItemsArray = [NSMutableArray array];
        for (NSUInteger i = 0; i < collection.numberOfEntries; i++)
        {
            [self.folderItemsArray addObject:[collection modelAtIndex:i]];

            NSLog(@"FOLDER NAME %@", innerFolderName);

            BoxItem *item = (BoxItem *)[self.folderItemsArray objectAtIndex:i];

                NSLog(@"Here The list of Files/Directories:");
                NSLog(@"TypE %@", item.type);                    // [file/directory]
                NSLog(@"NAME %@", item.name);                    // filename

                NSLog(@"ID %@", item.ETag);                      // directory level [0 = root]
                NSLog(@"ID %@", item.modelID);                   //  metadata unique identifier [FolderID !]

                NSLog(@"SIZE %@", item.size);                    // in bytes
                NSLog(@"Modified Time %@", item.modifiedAt);     // in UTC format 

                NSLog(@"HASH %lu", (unsigned long)item.hash);   // hash
        }
        self.totalCount = [collection.totalCount integerValue];
        NSLog(@" Number of files/dirs in Level %ld", (long)self.totalCount);
    }

最终的结果是代码启动了无限循环的请求,并且不理解为什么,因为它应该在没有更多目录时停止。任何有关识别问题的帮助都非常感谢。

谢谢!

1 个答案:

答案 0 :(得分:0)

你应该避免使用属性self.folderItemsArray,因为你有一个递归方法,试试这个:

- (NSArray*)fetchFolderItemsWithFolderID:(NSString *)innerFolder name:(NSString *)innerFolderName
{
  ...
      // This is a new line.
      NSMutableArray* folderItemsArray = [NSMutableArray array];
      [mgr folderItemsWithID:innerFolder requestBuilder:bldr success: ^(BoxCollection *collection)
  {
     //self.folderItemsArray = [NSMutableArray array]; remove this line.
     for (NSUInteger i = 0; i < collection.numberOfEntries; i++)
     {
        [folderItemsArray addObject:[collection modelAtIndex:i]]; // remove the self here.
     ...
   }
   return folderItemsArray;
}

并像这样更新此方法:

- (void)traverseItemsWithFolderID:(NSString *)folder name:(NSString *)namefolder

{   
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSArray* items = [self fetchFolderItemsWithFolderID:folder name:namefolder]; //   <- List Files/dirs in directory namefolder

        dispatch_async(dispatch_get_main_queue(), ^{

   // Create box object "item" to traverse folderItemsArray
    //BoxItem * item = (BoxItem *)[self.folderItemsArray objectEnumerator]; why do you need this line.
    for (BoxItem *item in items)
    {
        if([item.type isEqualToString:BoxAPIItemTypeFolder])
            {
                NSLog(@"SubFolder id %@", item.modelID);
                NSLog(@"SubFolder name %@", item.name);
                [self traverseItemsWithFolderID:item.modelID name:item.name];

            }
    }

    });
   });
}