iOS `scene:continueUserActivity:` deadlock when calling a synchronized API

What I need

I want to convert an async system API to a synchronized one and call it inside SceneDelegate’s continueUserActivity.

Note: I fully understand that iOS will kill the app if I don’t return within a few seconds. But in my case I have to wait till the system API completion in order to continue this function.

What’s working

I have the following code:

// Imagine this is a system async API that (1) has to be called on main thread, and (2) it also completes on main thread.
- (void)systemAsyncAPI:(void(^)(void))completion {
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    completion();
  });
}

// Convert the system API from async to sync
- (void)synchronizedSystemAPI {
  __block BOOL completed = NO;
  
  [self systemAsyncAPI:^{
    completed = YES;
  }];
  
  while (!completed) {
    NSLog(@"Tick");
    [NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
  }
  NSLog(@"completed");
}

Now when I use this code inside SceneDelegate’s willConnectToSession:

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
  [self synchronizedSystemAPI];
}

Everything works as expected, with the following log:

Tick
Tick
Tick
completed

What’s not working

Now, I need to call this synchronizedSystemAPI inside SceneDelegate‘s continueUserActivity instead of willConnectToSession.

- (void)scene:(UIScene *)scene continueUserActivity:(NSUserActivity *)userActivity {
  [self synchronizedSystemAPI];

  // Continue work after system API is complete
}

This code now has a deadlock, as it ticks forever:

Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
...

My understanding

Inspired by this answer, I think the reason is, willConnectToSession is called in old technique (predates GCD) that is not a dispatch item. continueUserActivity is part of a dispatch item on main queue, hence the deadlock, as explained in that linked answer.

How do I test the code?

An easy way to invoke continueUserActivity is via spotlight search:

  1. add NSUserActivityTypes array in info.plist. Here I put a single item “foo”.
  1. setup the activity:
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
  NSUserActivity *activity = [[NSUserActivity alloc] initWithActivityType:@"foo"];
  activity.title = @"foo title";
  activity.eligibleForSearch = YES;
  [activity becomeCurrent];
}
  1. Run the app.
  2. Go to home screen and search “foo title” in spotlight search.
  3. You should be able to this app. Tap on it to resume
  4. Notice continueUserActivity will be called.

And here is my full reproducible code. Don’t forget to add the activity item in Info.plist as described above.


@implementation SceneDelegate



// A system async API that has to be called on main thread, and it also completes on main thread.
- (void)systemAsyncAPI:(void(^)(void))completion {
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    completion();
  });
}

// Convert the above system API from async to sync
- (void)synchronizedSystemAPI {
  __block BOOL completed = NO;
  
  // Imagine this is a system API that must be called on main thread, and completes on main thread.
  [self systemAsyncAPI:^{
    completed = YES;
  }];
  
  while (!completed) {
    NSLog(@"Tick");
    [NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
  }
  NSLog(@"completed");
}


- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
  NSUserActivity *activity = [[NSUserActivity alloc] initWithActivityType:@"foo"];
  activity.title = @"foo title";
  activity.eligibleForSearch = YES;
  [activity becomeCurrent];
}

- (void)scene:(UIScene *)scene continueUserActivity:(NSUserActivity *)userActivity {
  [self synchronizedSystemAPI];
}
@end

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật