Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSManagedObjectContextのsave関連メソッドでNSErrorを返すように修正 #20

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/UBICoreData/NSManagedObjectContext+UBICoreData.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ NS_ASSUME_NONNULL_BEGIN
- (NSManagedObjectContext *)newPrivateQueueContext;

- (BOOL)save;
- (BOOL)saveWithError:(NSError **)error;
- (BOOL)saveToPersistentStore;
- (BOOL)saveToPersistentStoreWithError:(NSError **)error;

@end

Expand Down
24 changes: 19 additions & 5 deletions Sources/UBICoreData/NSManagedObjectContext+UBICoreData.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,38 @@ - (NSManagedObjectContext *)newPrivateQueueContext
}

- (BOOL)save
{
return [self saveWithError:nil];
}

- (BOOL)saveWithError:(NSError **)error
{
if (![self hasChanges]) {
return YES;
}

NSError* error = nil;
NSError* localError = nil;

if (![self save:&error]) {
if (![self save:&localError]) {
#ifdef DEBUG
NSLog(@"%s - error: %@", __PRETTY_FUNCTION__, [error userInfo]);
NSLog(@"%s - error: %@", __PRETTY_FUNCTION__, [localError userInfo]);
#endif
if (error) {
*error = localError;
}

return NO;
}

return YES;
}

- (BOOL)saveToPersistentStore
{
return [self saveToPersistentStoreWithError:nil];
}

- (BOOL)saveToPersistentStoreWithError:(NSError **)error
{
NSSet *insertedObjects = self.insertedObjects;
if (insertedObjects.count > 0) {
Expand All @@ -58,7 +72,7 @@ - (BOOL)saveToPersistentStore
}
}

if ([self save]) {
if ([self saveWithError:error]) {
NSManagedObjectContext *context = [self parentContext];

if (!context) {
Expand All @@ -68,7 +82,7 @@ - (BOOL)saveToPersistentStore
__block BOOL save = NO;

[context performBlockAndWait:^{
save = [context saveToPersistentStore];
save = [context saveToPersistentStoreWithError:error];
}];

return save;
Expand Down
Loading