[Sandbox] Synchronize write accesses to storage

Synchronize writing to the bookmark storage to the main thread.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
swiftingly
Christopher Snowhill 2022-06-29 19:41:03 -07:00
parent 4ce180fb2a
commit d739e68e8e
1 changed files with 22 additions and 18 deletions

View File

@ -212,6 +212,14 @@ static SandboxBroker *kSharedSandboxBroker = nil;
return nil;
}
static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_block_t block) {
if(dispatch_queue_get_label(queue) == dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)) {
block();
} else {
dispatch_sync(queue, block);
}
}
- (void)addFolderIfMissing:(NSURL *)folderUrl {
if(![folderUrl isFileURL]) return;
@ -237,6 +245,7 @@ static SandboxBroker *kSharedSandboxBroker = nil;
return;
}
dispatch_sync_reentrant(dispatch_get_main_queue(), ^{
NSPersistentContainer *pc = [NSClassFromString(@"PlaylistController") sharedPersistentContainer];
SandboxToken *token = [NSEntityDescription insertNewObjectForEntityForName:@"SandboxToken" inManagedObjectContext:pc.viewContext];
@ -244,11 +253,8 @@ static SandboxBroker *kSharedSandboxBroker = nil;
if(token) {
token.path = [folderUrl path];
token.bookmark = bookmark;
[pc.viewContext save:&err];
if(err) {
ALog(@"Error saving bookmark: %@", [err localizedDescription]);
}
}
});
}
}
}
@ -285,17 +291,15 @@ static SandboxBroker *kSharedSandboxBroker = nil;
NSPersistentContainer *pc = [NSClassFromString(@"PlaylistController") sharedPersistentContainer];
dispatch_sync_reentrant(dispatch_get_main_queue(), ^{
SandboxToken *token = [NSEntityDescription insertNewObjectForEntityForName:@"SandboxToken" inManagedObjectContext:pc.viewContext];
if(token) {
token.path = [url path];
token.bookmark = bookmark;
token.folder = NO;
[pc.viewContext save:&err];
if(err) {
ALog(@"Error saving bookmark: %@", [err localizedDescription]);
}
}
});
}
}
}