Skip to content

Commit

Permalink
Merge pull request #198 from blueshift-labs/release/v2.1.16
Browse files Browse the repository at this point in the history
Release/v2.1.16
  • Loading branch information
ketanshikharebsft authored Mar 25, 2021
2 parents 0498205 + 2645e6d commit 5e38546
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 326 deletions.
74 changes: 45 additions & 29 deletions BlueShift-iOS-SDK/BatchEventEntity.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,30 @@ - (void)insertEntryParametersList:(NSArray *)parametersArray andNextRetryTimeSta
@try {
if(context && [context isKindOfClass:[NSManagedObjectContext class]]) {
[context performBlock:^{
NSError *error = nil;
[context save:&error];
if(masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) {
[masterContext performBlock:^{
NSError *error = nil;
if (masterContext) {
[masterContext save:&error];
}
}];
@try {
NSError *error = nil;
[context save:&error];
if(masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) {
[masterContext performBlock:^{
@try {
NSError *error = nil;
if (masterContext) {
[masterContext save:&error];
}
} @catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
}
}];
}
} @catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
}
}];
}
}
@catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
}
} else {
return ;
}
}

Expand Down Expand Up @@ -104,17 +110,22 @@ + (void *)fetchBatchesFromCoreDataWithCompletetionHandler:(void (^)(BOOL, NSArra

+ (void *)fetchBatchesFromCoreDataFromContext:(NSManagedObjectContext*) context request: (NSFetchRequest*)fetchRequest handler:(void (^)(BOOL, NSArray *))handler {
NSNumber *currentTimeStamp = [NSNumber numberWithDouble:[[[NSDate date] dateByAddingMinutes:kRequestRetryMinutesInterval] timeIntervalSince1970]];
NSPredicate *nextRetryTimeStampLessThanCurrentTimePredicate = [NSPredicate predicateWithFormat:@"nextRetryTimeStamp < %@", currentTimeStamp];
[fetchRequest setPredicate:nextRetryTimeStampLessThanCurrentTimePredicate];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nextRetryTimeStamp < %@", currentTimeStamp];
[fetchRequest setPredicate:predicate];
@try {
if(context && [context isKindOfClass:[NSManagedObjectContext class]]) {
[context performBlock:^{
NSError *error;
NSArray *results = [[NSArray alloc]init];
results = [context executeFetchRequest:fetchRequest error:&error];
if (results && results.count > 0) {
handler(YES, results);
} else {
@try {
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);
}
} @catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
handler(NO, nil);
}
}];
Expand All @@ -124,6 +135,7 @@ + (void *)fetchBatchesFromCoreDataFromContext:(NSManagedObjectContext*) context
}
@catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
handler(NO, nil);
}
}

Expand All @@ -140,17 +152,21 @@ + (void)eraseEntityData {
NSBatchDeleteRequest *deleteRequest = [[NSBatchDeleteRequest alloc] initWithFetchRequest:fetchRequest];
if([batchContext isKindOfClass:[NSManagedObjectContext class]]) {
[batchContext performBlock:^{
NSError *error = nil;
// check if there are any changes to be saved and save it
if ([batchContext hasChanges]) {
@try {
NSError *error = nil;
// check if there are any changes to be saved and save it
if ([batchContext hasChanges]) {
[batchContext save:&error];
}
NSBatchDeleteResult* deleteResult = [batchContext executeRequest:deleteRequest error:&error];
[batchContext save:&error];
}
NSBatchDeleteResult* deleteResult = [batchContext executeRequest:deleteRequest error:&error];
[batchContext save:&error];
if (error) {
[BlueshiftLog logError:error withDescription:@"Failed to save the data after deleting events." methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
} else {
[BlueshiftLog logInfo:[NSString stringWithFormat:@"Deleted %@ records from the BatchEventEntity entity", deleteResult.result] withDetails:nil methodName:nil];
if (error) {
[BlueshiftLog logError:error withDescription:@"Failed to save the data after deleting events." methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
} else {
[BlueshiftLog logInfo:[NSString stringWithFormat:@"Deleted %@ records from the BatchEventEntity entity", deleteResult.result] withDetails:nil methodName:nil];
}
} @catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
}
}];
}
Expand Down
50 changes: 28 additions & 22 deletions BlueShift-iOS-SDK/BlueShiftAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -1129,30 +1129,36 @@ - (NSManagedObjectModel *)managedObjectModel {

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
if (_persistentStoreCoordinator != nil) {
NSString* key = [NSString stringWithUTF8String:__PRETTY_FUNCTION__];
@synchronized (key) {
@try {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}

// Create the coordinator and store
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"BlueShift-iOS-SDK.sqlite"];
NSError *error = nil;
NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES};
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
// Report any error we got.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
[BlueshiftLog logError:error withDescription:@"Unresolved error while creating persistent store coordinator" methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
}
} @catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
}

return _persistentStoreCoordinator;
}

// Create the coordinator and store

_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"BlueShift-iOS-SDK.sqlite"];
NSError *error = nil;
NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES};
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
// Report any error we got.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
[BlueshiftLog logError:error withDescription:@"Unresolved error" methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
}

return _persistentStoreCoordinator;
}

- (NSManagedObjectContext *)managedObjectContext {
Expand Down
11 changes: 8 additions & 3 deletions BlueShift-iOS-SDK/BlueShiftDeviceData.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@ - (NSString *)deviceType {
}

- (NSString *)networkCarrierName {
CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netinfo subscriberCellularProvider];
return [carrier carrierName];
// Skip fetching network carrier for simulator
#if TARGET_OS_SIMULATOR
return nil;
#else
CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netinfo subscriberCellularProvider];
return [carrier carrierName];
#endif
}

- (NSString *)operatingSystem {
Expand Down
76 changes: 51 additions & 25 deletions BlueShift-iOS-SDK/BlueShiftHttpRequestBatchUpload.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,32 @@ + (void)createBatchesWithContext: (NSManagedObjectContext*)masterContext withBat
}
}
[self createBatch:paramsArray];
}
}
@try {
if (context && [context isKindOfClass:[NSManagedObjectContext class]]) {
[context performBlock:^{
NSError *saveError = nil;
[context save:&saveError];
[masterContext performBlock:^{
NSError *saveError = nil;
if (masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) {
[masterContext save:&saveError];
if (context && [context isKindOfClass:[NSManagedObjectContext class]]) {
[context performBlockAndWait:^{
@try {
NSError *saveError = nil;
if ([context hasChanges]) {
[context save:&saveError];
}
[masterContext performBlockAndWait:^{
@try {
NSError *saveError = nil;
if (masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) {
[masterContext save:&saveError];
}
}
@catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
}
}];
}
@catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
}
}];
}];
}
}
}
@catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
}
}


Expand Down Expand Up @@ -216,32 +223,42 @@ + (void) deleteBatchRecords:(BatchEventEntity *)batchEvent context:(NSManagedObj
@try {
if(context && [context isKindOfClass:[NSManagedObjectContext class]]) {
[context performBlock:^{
[context deleteObject:batchEvent];
[context performBlock:^{
@try {
[context deleteObject:batchEvent];
NSError *saveError = nil;
if(context && [context isKindOfClass:[NSManagedObjectContext class]]) {
if(context) {
[context save:&saveError];
}
_requestQueueStatus = BlueShiftRequestQueueStatusAvailable;
}];
handler(YES);
} @catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
_requestQueueStatus = BlueShiftRequestQueueStatusAvailable;
handler(NO);
}
}];
}
else {
_requestQueueStatus = BlueShiftRequestQueueStatusAvailable;
handler(NO);
}
}
@catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
_requestQueueStatus = BlueShiftRequestQueueStatusAvailable;
handler(NO);
}
handler(YES);
}

+ (void) handleRetryBatchUpload:(BatchEventEntity *)batchEvent context:(NSManagedObjectContext*)context requestOperation: (BlueShiftBatchRequestOperation*)requestOperation completetionHandler:(void (^)(BOOL))handler {
// Request is not executed due to some reasons ...
@try {
if(context && [context isKindOfClass:[NSManagedObjectContext class]]) {
[context performBlock:^{
[context deleteObject:batchEvent];
[context performBlock:^{
@try {
[context deleteObject:batchEvent];
NSError *saveError = nil;
if(context && [context isKindOfClass:[NSManagedObjectContext class]]) {
if(context) {
[context save:&saveError];
NSInteger retryAttemptsCount = requestOperation.retryAttemptsCount;
requestOperation.retryAttemptsCount = retryAttemptsCount - 1;
Expand All @@ -254,14 +271,23 @@ + (void) handleRetryBatchUpload:(BatchEventEntity *)batchEvent context:(NSManage
}
}
_requestQueueStatus = BlueShiftRequestQueueStatusAvailable;
}];
handler(YES);
} @catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
_requestQueueStatus = BlueShiftRequestQueueStatusAvailable;
handler(NO);
}
}];
} else {
_requestQueueStatus = BlueShiftRequestQueueStatusAvailable;
handler(NO);
}
}
@catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
_requestQueueStatus = BlueShiftRequestQueueStatusAvailable;
handler(NO);
}
handler(NO);
}


Expand Down
11 changes: 11 additions & 0 deletions BlueShift-iOS-SDK/BlueShiftNotificationConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,15 @@

#define kBsftEncodedSpace @"%20"

#define kInAppTriggerModeNow @"now"
#define kInAppTriggerModeUpcoming @"upcoming"
#define kInAppTriggerModeEvent @"event"
#define kInAppTriggerModeNowAndUpcoming @"NowAndUpComing"

#define kInAppPriorityMedium @"medium"
#define kInAppId @"id"

#define kInAppStatus @"status"
#define kInAppStatusPending @"pending"

#endif
3 changes: 0 additions & 3 deletions BlueShift-iOS-SDK/BlueShiftRequestQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,4 @@
// Method to add Batch Request Operation to Queue ....
+ (void)addBatchRequestOperation:(BlueShiftBatchRequestOperation *)requestOperation;

// Method to set the request queue status explicity ...
+ (void)setRequestQueueStatus:(BlueShiftRequestQueueStatus)requestQueueStatus;

@end
Loading

0 comments on commit 5e38546

Please sign in to comment.