Question

I am using the AssetsLibrary framework to retrieve all asset groups in the iPhone. I have some code snippet like this:

NSMutableArray *groups = [[NSMutableArray alloc] initWithCapacity:5];
void (^groupEnumerator)(ALAssetsGroup*, BOOL*) = ^(ALAssetsGroup *group, BOOL *stop) {
    if(group != nil) {
        NSLog(@"Adding group %@", [group valueForProperty:ALAssetsGroupPropertyName]);
        [groups addObject:group];
    }
};

ALAssetsLibrary* library = [AssetsManager defaultAssetsLibrary];
[library enumerateGroupsWithTypes: ALAssetsGroupSavedPhotos
    usingBlock:groupEnumerator
    failureBlock:^(NSError * err) {NSLog(@"Erorr: %@", [err localizedDescription]);}];

And I found a weird behavior:

1) When I run my app in the iOS simulator (use command + R in Xcode), the groupEnumerator will be executed and group names will be printed in the console

2) I wrote some unit test for my app, when I run the unit test (use command + U in Xcode), which calls the above code snippet as it is called in my app, the groupEnumerator seems not get executed at all and none of the group name will be printed, and the failureBlock doesn't get called too and I don't get any error when running the tests but the group enumeration doesn't work as it does in the app.

I check Apple's documentation, and I know this method (enumerateGroupsWithTypes:usingBlock:failureBlock:) is asynchronous, however, even if I wait for a long time in my unit test, I don't see the groupEnumerator get executed.

I am testing against iOS 5.0 with Xcode 4.2.

Any help is appreciated. Thanks.

Was it helpful?

Solution

Stuck on this problem for several days, couldn't figure out the exact cause of this issue.

And instead of using OCUnit in Xcode 4, I had to switch to use gh-unit, which solves this issue actually. It seems related with the way Xcode 4 runs the test target.

http://gabriel.github.com/gh-unit

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top