Skip to content

Commit

Permalink
🔥 Get rid of try catch when toggling favorite on Darwin
Browse files Browse the repository at this point in the history
To avoid crashes on specific Xcode versions
  • Loading branch information
AlexV525 committed Oct 22, 2024
1 parent bd48e50 commit 4a1cc05
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
13 changes: 7 additions & 6 deletions ios/Classes/PMPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -613,12 +613,13 @@ - (void)handleMethodResultHandler:(ResultHandler *)handler manager:(PMManager *)
} else if ([@"favoriteAsset" isEqualToString:call.method]) {
NSString *id = call.arguments[@"id"];
BOOL favorite = [call.arguments[@"favorite"] boolValue];
@try {
BOOL favoriteResult = [manager favoriteWithId:id favorite:favorite];
[handler reply:@(favoriteResult)];
} @catch (NSObject *error) {
[handler replyError:error];
}
[manager favoriteWithId:id favorite:favorite block:^(BOOL result, NSObject *error) {
if (error) {
[handler replyError:error];
} else {
[handler reply:@(result)];
}
}];
} else if ([@"requestCacheAssetsThumb" isEqualToString:call.method]) {
NSArray *ids = call.arguments[@"ids"];
PMThumbLoadOption *option = [PMThumbLoadOption optionDict:call.arguments[@"option"]];
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/core/PMManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ typedef void (^AssetBlockResult)(PMAssetEntity *, NSObject *);

- (void)removeCollectionWithId:(NSString *)id type:(int)type block:(void (^)(NSObject *))block;

- (BOOL)favoriteWithId:(NSString *)id favorite:(BOOL)favorite;
- (void)favoriteWithId:(NSString *)id favorite:(BOOL)favorite block:(void (^)(BOOL result, NSObject *))block;

- (void)clearFileCache;

Expand Down
19 changes: 11 additions & 8 deletions ios/Classes/core/PMManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -1625,30 +1625,33 @@ - (void)removeCollectionWithId:(NSString *)id type:(int)type block:(void (^)(NSO
}
}

- (BOOL)favoriteWithId:(NSString *)id favorite:(BOOL)favorite {
- (void)favoriteWithId:(NSString *)id favorite:(BOOL)favorite block:(void (^)(BOOL result, NSObject *error))block {
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[id] options:nil];
PHAsset *asset = [self getFirstObjFromFetchResult:fetchResult];
if (!asset) {
@throw [NSString stringWithFormat:@"Asset %@ not found.", id];
block(NO, [NSString stringWithFormat:@"Asset %@ not found.", id]);
return;
}

if (![asset canPerformEditOperation:PHAssetEditOperationProperties]) {
@throw [NSString stringWithFormat:@"The asset %@ cannot perform edit operation.", id];
block(NO, [NSString stringWithFormat:@"The asset %@ cannot perform edit operation.", id]);
return;
}

NSError *error;
BOOL succeed = [PHPhotoLibrary.sharedPhotoLibrary
performChangesAndWait:^{
BOOL succeed = [PHPhotoLibrary.sharedPhotoLibrary performChangesAndWait:^{
PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:asset];
request.favorite = favorite;
} error:&error];
if (!succeed) {
@throw [NSString stringWithFormat:@"Favouring asset %@ failed: Request not succeed.", id];
block(NO, [NSString stringWithFormat:@"Favouring asset %@ failed: Request not succeed.", id]);
return;
}
if (error) {
@throw error;
block(NO, error);
return;
}
return YES;
block(YES, nil);
}

- (NSString *)getCachePath:(NSString *)type {
Expand Down

0 comments on commit 4a1cc05

Please sign in to comment.