![](/img/trans.png)
[英]Wait for method with completion block to finish inside fast enumeration block
[英]Call block method and wait for user to finish then get respond back
我正在使用以下塊方法調用照片選擇器,但我想等待用戶輸入然后得到響應...這怎么可能?
我使用下面的代碼調用該方法
[self callmethod:[PHPhotoLibrary sharedPhotoLibrary] withCompletion:^(BOOL success, NSError *error, id responce) {
if(success)
{completion(prevStatus == PHAuthorizationStatusAuthorized);
NSLog(@"==%@",responce); // here i get response once user selected photos
}
}];
我使用前面的代碼調用的方法
-(void)callmethod:(PHPhotoLibrary*)sharedPhotoLibrary withCompletion:(void(^)(BOOL success, NSError* error, id responce))completion
{
if (@available(iOS 14, *)) {
[sharedPhotoLibrary presentLimitedLibraryPickerFromViewController:self];
}
if (completion){
dispatch_async(dispatch_get_main_queue(), ^{
completion(YES,nil,sharedPhotoLibrary); // here that call when method complete
});
}
}
在 iOS 15 中,有一個采用完成處理程序的再現。 但是在我的測試中,有些情況下它沒有被調用(即沒有變化)或被多次調用(。),恕我直言。 違反了“完成處理程序”非正式合同,此外,即 iOS 15. 僅,所以。 我根本不傾向於使用完成處理程序模式。
正如 WWDC 2020 視頻Handle the Limited Photos Library in your app 中所建議的,您可以使用PHPhotoLibraryChangeObserver
。 例如,如果視圖 controller:
@interface ViewController () <PHPhotoLibraryChangeObserver>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
...
}
- (void)dealloc {
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
}
- (void)photoLibraryDidChange:(nonnull PHChange *)changeInstance {
...
}
@end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.