开发者

iOS 4 Assets Libraries problem: NSMutableArray contains data in Block, but is empty after Block calls

I'm trying to incorporate the example found on http://www.icodeblog.com/asset-libraries-and-blocks-in-ios-4/ into my own app. This is what I have so far:

@interface UserTabBarController : UIViewController <NSFetchedResultsControllerDelegate, UITabBarDelegate> {
    NSMutableArray *assets;
}
    -(void)usingAssets;
@end

@implementation UserTabBarController
    -(void)usingAssets{

        void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
            if(result != NULL) {
            NSLog(@"See Asset: %@", result);
            [assets addObject:result];
            NSLog(@"assets count: %i", assets.count);
            }
            else {
            NSLog(@"AssetEnum: result nil or end of list");
            }
        };

        void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) {
 开发者_开发问答       if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            }
            else {
            NSLog(@"GroupEnum: group nil or end of list");
            }
            NSLog(@"assets count in GroupEnum: %i", assets.count);
    };


        ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

        [library enumerateGroupsWithTypes:ALAssetsGroupAll
                               usingBlock:assetGroupEnumerator
                                failureBlock: ^(NSError *error) {
                   NSLog(@"Failure: %@", error.description);
            }];
        [library release];
    }

Let's (hopefully) agree that everytime I call usingAssets, I add the latest content of the Photo album to the NSMutableArray* assets. I pull all this together in my ViewController's ViewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
    assets = [[NSMutableArray alloc] init];
    [self usingAssets];
    for (ALAsset* a in self.assets){
        NSLog(@"Item in asset");
    }
    NSLog(@"Asset count: %i", assets.count);

    [self usingAssets];
    NSLog(@"Asset count: %i", assets.count);

}

So far, I believe I've followed the example shown in the link above. The problem is that within the scope of UserTabBarController, assets.count remains at 0, while within the scope of the Block, it shows the count as increasing, and it persists between both calls (as I would have expected). What I also find strange is that the lines Asset count: 0, should be appearing much later in the log, or maybe the lines of seeing assets (which are called from within the block) should be happening sooner:

2011-02-21 14:22:59.140 iPCS[7219:207] Asset count: 0

2011-02-21 14:23:08.539 iPCS[7219:207] Asset count: 0

2011-02-21 14:14:03.363 iPCS[7139:207] See Asset: ALAsset - Type:Photo, URLs:{ "public.jpeg" = "assets-library://asset/asset.JPG?id=1000000001&ext=JPG"; }

2011-02-21 14:14:03.363 iPCS[7139:207] assets count: 1

2011-02-21 14:14:03.365 iPCS[7139:207] See Asset: ALAsset - Type:Photo, URLs:{ "public.jpeg" = "assets-library://asset/asset.JPG?id=1000000002&ext=JPG"; }

2011-02-21 14:14:03.366 iPCS[7139:207] assets count: 2

2011-02-21 14:14:03.367 iPCS[7139:207] AssetEnum: result nil or end of list

2011-02-21 14:14:03.367 iPCS[7139:207] assets count in GroupEnum: 2

2011-02-21 14:14:03.368 iPCS[7139:207] GroupEnum: group nil or end of list

2011-02-21 14:14:03.368 iPCS[7139:207] assets count in GroupEnum: 2

2011-02-21 14:14:03.370 iPCS[7139:207] See Asset: ALAsset - Type:Photo, URLs:{ "public.jpeg" = "assets-library://asset/asset.JPG?id=1000000001&ext=JPG"; }

2011-02-21 14:14:03.371 iPCS[7139:207] assets count: 3

2011-02-21 14:14:03.373 iPCS[7139:207] See Asset: ALAsset - Type:Photo, URLs:{ "public.jpeg" = "assets-library://asset/asset.JPG?id=1000000002&ext=JPG"; }

2011-02-21 14:14:03.374 iPCS[7139:207] assets count: 4

2011-02-21 14:14:03.374 iPCS[7139:207] AssetEnum: result nil or end of list

2011-02-21 14:14:03.375 iPCS[7139:207] assets count in GroupEnum: 4

2011-02-21 14:14:03.375 iPCS[7139:207] GroupEnum: group nil or end of list

2011-02-21 14:14:03.376 iPCS[7139:207] assets count in GroupEnum: 4

What say you all?

Edit: Ran a few more tests, and called usingAssets using a button on the view, and the assets array was populated properly! This means that at load-time, during the viewDidLoad, there is some sort of disconnect between the object as seen in the block, and the object as seen in the block's calling instance. Anyone have any ideas as to why it is exhibiting this behaviour?

Edit 2: Been playing with the code for over a week now, and I am still unable to get the assets NSArray to accurately show content. I've determine it must be tied to the order of which the blocks are executed. When I call usingAssets, the method itself completes, but the blocks don't run right away. So, say I tie the method to a button, and immediately print the contents of assets, its empty (I have 2 images in the library). I click the button again, now the contents show 2 items. If I click it a third time, it shows 4. It is almost as if the Blocks run after control is returned to the user (back to the application loop) I'm on the verge of giving up, scrapping my assets code and just loading them based on the path they exist in, but I'd rather use assets, if I can figure out why it is not behaving as expected


What I did for this, is to check for when the group is nil to see when it has finished enumerating, then you can check the count...

if(group == nil) { 
    NSLog('Assets finished renumerating, %i found', [self.assets count]); 
} else { 
    [group enumerate...]; 
}

Also, if you are using assets as a property, then use self.assets throughout so it gets updated. This code is now working for me :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜