Cocoa thread synchronisation when using [ALAssetsLibrary enumerateGroupsWithTypes:]

后端 未结 3 1721
别跟我提以往
别跟我提以往 2020-12-14 23:39

I have recently, like a few people, discovered that [ALAssetsLibrary enumerateGroupsWithTypes] likes to run its blocks on another thread. What a shame that Apple di

3条回答
  •  别那么骄傲
    2020-12-14 23:42

    The framework doesn't run these blocks on a separate thread. It just runs them as additional events in the same run-loop. To prove it, try this

        [library enumerateGroupsWithTypes:ALAssetsGroupAll 
                               usingBlock:[^(ALAssetsGroup * group, BOOL * stop)
                                 {
                                   if([NSThread isMainThread])
                                   {
                                      NSLog(@"main");
                                   }
                                   else
                                   {
                                     NSLog(@"non-main");
                                   }
                                 } copy] 
               failureBlock:^(NSError * err)
                              {NSLog(@"Erorr: %@", [err localizedDescription] );}];
        [library release];
        if([NSThread isMainThread])
        {
            NSLog(@"main");
        }
        else
        {
            NSLog(@"non-main");
        }
    

    My output from this was

    main
    main
    main
    

    Meaning that the block was being called in the main thread. It's just a separate event. To solve your problem, you just need to return your value somehow from within the block when you reach the last step. You can tell it's the last step because your block will be called with nil for the group object.

    EDIT: for instance use this block

    ^(ALAssetsGroup * group, BOOL * stop)
    {
        if(group == nil)
        {
            // we've enumerated all the groups 
            // do something to return a value somehow (maybe send a selector to a delegate)
        }
    }
    

提交回复
热议问题