Skip to content

Commit

Permalink
Added support for persistent container from iOS 10
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpjswl committed Jan 31, 2022
1 parent 9ec45f1 commit 6d45f78
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions ASJCoreDataOperation/ASJCoreDataOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import "ASJCoreDataOperation.h"
#import <UIKit/UIApplication.h>
#import <CoreData/NSManagedObjectContext.h>
#import <CoreData/NSPersistentContainer.h>

@interface ASJCoreDataOperation ()

Expand Down Expand Up @@ -93,12 +94,27 @@ - (NSManagedObjectContext *)appDelegateMoc
});

SEL managedObjectContext = NSSelectorFromString(@"managedObjectContext");
NSAssert([appDelegate respondsToSelector:managedObjectContext], @"If managedObjectContext is not present in AppDelegate, you must provide one that operates on the main queue while initializing the operation.");
if ([appDelegate respondsToSelector:managedObjectContext])
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [appDelegate performSelector:managedObjectContext];
#pragma clang diagnostic pop
}

SEL persistentContainer = NSSelectorFromString(@"persistentContainer");
if ([appDelegate respondsToSelector:persistentContainer])
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [appDelegate performSelector:managedObjectContext];
NSPersistentContainer *container = [appDelegate performSelector:persistentContainer];
return container.viewContext;
#pragma clang diagnostic pop
}

NSAssert(NO, @"Error: No 'managedObjectContext' found that operates on the main queue.\nSolution: Make sure that a 'managedObjectContext' or 'persistentContainer' is present in AppDelegate or pass a 'managedObjectContext' while initializing the operation.");

return nil;
}

#pragma mark - Notifications
Expand Down

0 comments on commit 6d45f78

Please sign in to comment.