Skip to content

Commit

Permalink
Merge pull request #27 from blueshift-labs/core_data_in_background_th…
Browse files Browse the repository at this point in the history
…read

Core data in background thread
  • Loading branch information
anwarshahas authored Jun 5, 2017
2 parents 5f038fc + cb49da4 commit 1f6ebc4
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 290 deletions.
4 changes: 2 additions & 2 deletions BlueShift-iOS-Extension-SDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "BlueShift-iOS-Extension-SDK"
s.version = "0.1.6"
s.version = "0.1.7"
s.summary = "iOS SDK for push notification content extension and service extension for integrating media and carousel push notifications"

s.description = <<-DESC
Expand Down Expand Up @@ -80,7 +80,7 @@ Pod::Spec.new do |s|
# Supports git, hg, bzr, svn and HTTP.
#

s.source = { :git => "https://github.com/blueshift-labs/BlueShift-iOS-SDK.git", :tag => "0.1.6" }
s.source = { :git => "https://github.com/blueshift-labs/BlueShift-iOS-SDK.git", :tag => "0.1.7" }


# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
4 changes: 2 additions & 2 deletions BlueShift-iOS-SDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "BlueShift-iOS-SDK"
s.version = "0.1.6"
s.version = "0.1.7"
s.summary = "iOS SDK for integrating push notification and analytics"

s.description = <<-DESC
Expand Down Expand Up @@ -78,7 +78,7 @@ Pod::Spec.new do |s|
# Supports git, hg, bzr, svn and HTTP.
#

s.source = { :git => "https://github.com/blueshift-labs/BlueShift-iOS-SDK.git", :tag => "0.1.6" }
s.source = { :git => "https://github.com/blueshift-labs/BlueShift-iOS-SDK.git", :tag => "0.1.7 }
# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
2 changes: 1 addition & 1 deletion BlueShift-iOS-SDK/BatchEventEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@


// Method to return the batch records from Core Data ...
+ (NSArray *)fetchBatchesFromCoreData;
+ (void *)fetchBatchesFromCoreDataWithCompletetionHandler:(void (^)(BOOL, NSArray *))handler;

@end
70 changes: 43 additions & 27 deletions BlueShift-iOS-SDK/BatchEventEntity.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,75 @@ @implementation BatchEventEntity
// Method to insert Entry for a particular request operation in core data ...

- (void)insertEntryParametersList:(NSArray *)parametersArray andNextRetryTimeStamp:(NSInteger)nextRetryTimeStamp andRetryAttemptsCount:(NSInteger)retryAttemptsCount {
//NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];

// return if context is unavailable ...

if (context == nil) {
BlueShiftAppDelegate * appDelegate = (BlueShiftAppDelegate *)[BlueShift sharedInstance].appDelegate;
if (appDelegate != nil && appDelegate.batchEventManagedObjectContext != nil) {
NSManagedObjectContext *masterContext = appDelegate.batchEventManagedObjectContext;
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
context.parentContext = masterContext;
// return if context is unavailable ...
if (context == nil || masterContext == nil) {
return ;
}
// will only archive parameters list if they are present to prevent crash ...
if (parametersArray) {
self.paramsArray = [NSKeyedArchiver archivedDataWithRootObject:parametersArray];
}
self.nextRetryTimeStamp = [NSNumber numberWithDouble:nextRetryTimeStamp];
self.retryAttemptsCount = [NSNumber numberWithInteger:retryAttemptsCount];
[context performBlock:^{
NSError *error = nil;
[context save:&error];
[masterContext performBlock:^{
NSError *error = nil;
[masterContext save:&error];
}];
}];
} else {
return ;
}



// will only archive parameters list if they are present to prevent crash ...

if (parametersArray) {
self.paramsArray = [NSKeyedArchiver archivedDataWithRootObject:parametersArray];
}

self.nextRetryTimeStamp = [NSNumber numberWithDouble:nextRetryTimeStamp];
self.retryAttemptsCount = [NSNumber numberWithInteger:retryAttemptsCount];

NSError *error;
[context save:&error];
}


// Method to return the failed batch records from Core Data ....

+ (NSArray *)fetchBatchesFromCoreData {
+ (void *)fetchBatchesFromCoreDataWithCompletetionHandler:(void (^)(BOOL, NSArray *))handler {
@synchronized(self) {
NSArray *results = [[NSArray alloc]init];
BlueShiftAppDelegate *appDelegate = (BlueShiftAppDelegate *)[BlueShift sharedInstance].appDelegate;
if(appDelegate != nil && appDelegate.managedObjectContext != nil) {
NSManagedObjectContext *context = appDelegate.managedObjectContext;
if(appDelegate != nil && appDelegate.batchEventManagedObjectContext != nil) {
NSManagedObjectContext *context = appDelegate.batchEventManagedObjectContext;
if(context != nil) {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"BatchEventEntity" inManagedObjectContext:context]];
if(fetchRequest.entity != nil) {
NSNumber *currentTimeStamp = [NSNumber numberWithDouble:[[[NSDate date] dateByAddingMinutes:kRequestRetryMinutesInterval] timeIntervalSince1970]];
NSPredicate *nextRetryTimeStampLessThanCurrentTimePredicate = [NSPredicate predicateWithFormat:@"nextRetryTimeStamp < %@", currentTimeStamp];
[fetchRequest setPredicate:nextRetryTimeStampLessThanCurrentTimePredicate];
NSError *error;
@try {
if(context && [context isKindOfClass:[NSManagedObjectContext class]]) {
results = [context executeFetchRequest:fetchRequest error:&error];
[context performBlock:^{
NSError *error;
NSArray *results = [[NSArray alloc]init];
results = [context executeFetchRequest:fetchRequest error:&error];
if (results && results.count > 0) {
handler(YES, results);
} else {
handler(NO, nil);
}
}];
} else {
handler(NO, nil);
}
}
@catch (NSException *exception) {
NSLog(@"Caught exception %@", exception);
}
} else {
handler(NO, nil);
}
}
} else {
handler(NO, nil);
}
return results;
}
}

Expand Down
2 changes: 0 additions & 2 deletions BlueShift-iOS-SDK/BlueShift.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ - (void) setupWithConfiguration:(BlueShiftConfig *)config {
// assigning the current application delegate with the app delegate we are going to use in the SDK ...
_sharedBlueShiftInstance.appDelegate = _newDelegate;

// [UIApplication sharedApplication].delegate = _newDelegate;

// setting the new delegate's old delegate with the original delegate we saved...
BlueShiftAppDelegate *blueShiftAppDelegate = (BlueShiftAppDelegate *)_newDelegate;
blueShiftAppDelegate.oldDelegate = oldDelegate;
Expand Down
2 changes: 2 additions & 0 deletions BlueShift-iOS-SDK/BlueShiftAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
@property BlueShiftDeepLink * _Nullable deepLinkToCustomPage;

@property (readonly, strong, nonatomic) NSManagedObjectContext * _Nullable managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectContext * _Nullable realEventManagedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectContext * _Nullable batchEventManagedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel * _Nullable managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator * _Nullable persistentStoreCoordinator;

Expand Down
43 changes: 39 additions & 4 deletions BlueShift-iOS-SDK/BlueShiftAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,9 @@ - (void)appDidBecomeActive:(UIApplication *)application {
[self trackAppOpen];
// Uploading previous Batch events if anything exists
//To make the code block asynchronous
[BlueShiftHttpRequestBatchUpload batchEventsUploadInBackground];

if ([BlueShift sharedInstance].config.enableAnalytics) {
[BlueShiftHttpRequestBatchUpload batchEventsUploadInBackground];
}
// Will have to handled by SDK .....
}

Expand All @@ -774,7 +775,9 @@ - (void)appDidEnterBackground:(UIApplication *)application {

// Uploading Batch events
//To make the code block asynchronous
[BlueShiftHttpRequestBatchUpload batchEventsUploadInBackground];
if ([BlueShift sharedInstance].config.enableAnalytics) {
[BlueShiftHttpRequestBatchUpload batchEventsUploadInBackground];
}
}
}

Expand Down Expand Up @@ -1012,6 +1015,8 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
#pragma mark - Core Data stack

@synthesize managedObjectContext = _managedObjectContext;
@synthesize realEventManagedObjectContext = _realEventManagedObjectContext;
@synthesize batchEventManagedObjectContext = _batchEventManagedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

Expand Down Expand Up @@ -1081,11 +1086,41 @@ - (NSManagedObjectContext *)managedObjectContext {
if (!coordinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}

- (NSManagedObjectContext *)realEventManagedObjectContext {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_realEventManagedObjectContext != nil) {
return _realEventManagedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_realEventManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_realEventManagedObjectContext setPersistentStoreCoordinator:coordinator];
return _realEventManagedObjectContext;
}

- (NSManagedObjectContext *)batchEventManagedObjectContext {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_batchEventManagedObjectContext != nil) {
return _batchEventManagedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_batchEventManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_batchEventManagedObjectContext setPersistentStoreCoordinator:coordinator];
return _batchEventManagedObjectContext;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
Expand Down
Loading

0 comments on commit 1f6ebc4

Please sign in to comment.