Asynchronous vs Synchronous vs Threading in an iPhone App

后端 未结 8 1395
失恋的感觉
失恋的感觉 2021-01-30 05:28

I\'m in the design stage for an app which will utilize a REST web service and sort of have a dilemma in as far as using asynchronous vs synchronous vs threading. Here\'s the sce

8条回答
  •  梦谈多话
    2021-01-30 06:24

    I would use dispatch_async with synchronous. This way, you have the advantage of no delegates/NSNotifications and it is non-blocking

    - (NSArray *)users {
        if(users == nil) {
            users = do_sync_request();
        }
    
        return users;
    }
    
    // now when calling the users method, do this
    
    - (NSArray *)getUsers {
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
               NSArray *users = [self users];
               dispatch_sync(dispatch_get_main_queue(), ^{
                     return users;
               }
         }
    }
    

提交回复
热议问题