Skip to content

Commit

Permalink
Fix a minor bug on AWARECore
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuuki Nishiyama committed Jul 2, 2019
1 parent e622b79 commit 4bc1a24
Show file tree
Hide file tree
Showing 18 changed files with 1,078 additions and 528 deletions.
2 changes: 1 addition & 1 deletion AWAREFramework.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'AWAREFramework'
s.version = '1.4.15'
s.version = '1.4.16'
s.summary = 'AWARE: An Open-source Context Instrumentation Framework'

# This description is used to generate tags and improve search results.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="14490.98" systemVersion="18E226" minimumToolsVersion="Xcode 7.3" sourceLanguage="Objective-C" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="14490.99" systemVersion="18F132" minimumToolsVersion="Xcode 7.3" sourceLanguage="Objective-C" userDefinedModelVersionIdentifier="">
<entity name="EntityAccelerometer" representedClassName="EntityAccelerometer" syncable="YES">
<attribute name="accuracy" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="NO" syncable="YES"/>
<attribute name="device_id" optional="YES" attributeType="String" syncable="YES"/>
Expand Down
4 changes: 3 additions & 1 deletion AWAREFramework/Classes/Core/AWARECore.m
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,10 @@ - (void) requestPermissionForBackgroundSensingWithCompletion:(LocationAPIAuthori
[_sharedLocationManager requestAlwaysAuthorization];
}
}else{
if (self->completionHandler != nil) {
self->completionHandler();
}
self->completionHandler = nil;
completionHandler();
}
}

Expand Down
5 changes: 5 additions & 0 deletions AWAREFramework/Classes/Core/Storage/AWAREStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ NS_ASSUME_NONNULL_BEGIN

typedef void (^SyncProcessCallBack)(NSString * _Nonnull name, double progress, NSError * _Nullable error);
typedef void (^FetchDataHandler)(NSString * _Nonnull name, NSArray * results, NSDate * start, NSDate * end, NSError * _Nullable error);
typedef void (^LimitedDataFetchHandler)(NSString * _Nonnull name, NSArray * _Nullable results, NSDate * _Nonnull from, NSDate * _Nonnull to, BOOL isEnd, NSError * _Nullable error);

//////////////////// General //////////////////////

Expand Down Expand Up @@ -82,6 +83,10 @@ typedef void (^FetchDataHandler)(NSString * _Nonnull name, NSArray * results, NS

- (NSArray *) fetchDataBetweenStart:(NSDate *)start andEnd:(NSDate *)end;
- (void) fetchDataBetweenStart:(NSDate *)start andEnd:(NSDate *)end withHandler:(FetchDataHandler)handler;

- (NSArray *) fetchDataFrom:(NSDate *)from to:(NSDate *)to;
- (void) fetchDataFrom:(NSDate *)from to:(NSDate *)to handler:(FetchDataHandler)handler;
- (void) fetchDataFrom:(NSDate *)from to:(NSDate *)to limit:(int)limit all:(bool)all handler:(LimitedDataFetchHandler)handler;
- (NSDate *) getToday;

NS_ASSUME_NONNULL_END
Expand Down
12 changes: 12 additions & 0 deletions AWAREFramework/Classes/Core/Storage/AWAREStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,18 @@ - (void) fetchDataBetweenStart:(NSDate *)start andEnd:(NSDate *)end withHandler:
NSLog(@"Please overwrite -fetchDataBetweenStart:andEnd:withHandler method");
}

- (NSArray *)fetchDataFrom:(NSDate *)from to:(NSDate *)to{
NSLog(@"Please overwrite -fetchDataFrom:to method");
return @[];
}

- (void) fetchDataFrom:(NSDate *)from to:(NSDate *)to handler:(FetchDataHandler)handler{
NSLog(@"This storage which you are using does not support this method. Please overwrite -fetchDataFrom:to:handler method");
}

- (void)fetchDataFrom:(NSDate *)from to:(NSDate *)to limit:(int)limit all:(bool)all handler:(LimitedDataFetchHandler)handler{
NSLog(@"This storage which you are using does not support this method. Please overwrite -fetchDataFrom:to:limit:all:handler method");
}

- (NSDate *) getToday {
NSCalendar* calendar = [NSCalendar currentCalendar];
Expand Down
77 changes: 69 additions & 8 deletions AWAREFramework/Classes/Core/Storage/SQLite/SQLiteStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,12 @@ -(NSArray *)fetchTodaysData{
}

- (NSArray *)fetchDataBetweenStart:(NSDate *)start andEnd:(NSDate *)end{

NSNumber * startNum = [AWAREUtils getUnixTimestamp:start];
NSNumber * endNum = [AWAREUtils getUnixTimestamp:end];
return [self fetchDataFrom:start to:end];
}

- (NSArray *)fetchDataFrom:(NSDate *)from to:(NSDate *)to{
NSNumber * startNum = [AWAREUtils getUnixTimestamp:from];
NSNumber * endNum = [AWAREUtils getUnixTimestamp:to];

NSManagedObjectContext *moc = coreDataHandler.managedObjectContext;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
Expand All @@ -606,19 +609,22 @@ - (NSArray *)fetchDataBetweenStart:(NSDate *)start andEnd:(NSDate *)end{
return results;
}


- (void)fetchTodaysDataWithHandler:(FetchDataHandler)handler{
NSDate * today = [self getToday];
[self fetchDataBetweenStart:today andEnd:[today dateByAddingTimeInterval:60*60*24] withHandler:handler];
[self fetchDataFrom:today to:[today dateByAddingTimeInterval:60*60*24] handler:handler];
}

- (void)fetchDataBetweenStart:(NSDate *)start andEnd:(NSDate *)end withHandler:(FetchDataHandler)handler{
[self fetchDataFrom:start to:end handler:handler];
}

- (void)fetchDataFrom:(NSDate *)from to:(NSDate *)to handler:(FetchDataHandler)handler{
NSManagedObjectContext *private = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[private setParentContext:self.mainQueueManagedObjectContext];
[private performBlock:^{

NSNumber * startNum = [AWAREUtils getUnixTimestamp:start];
NSNumber * endNum = [AWAREUtils getUnixTimestamp:end];
NSNumber * startNum = [AWAREUtils getUnixTimestamp:from];
NSNumber * endNum = [AWAREUtils getUnixTimestamp:to];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:self->entityName];
[fetchRequest setEntity:[NSEntityDescription entityForName:self->entityName inManagedObjectContext:self.mainQueueManagedObjectContext]];
Expand All @@ -643,7 +649,62 @@ - (void)fetchDataBetweenStart:(NSDate *)start andEnd:(NSDate *)end withHandler:(
NSLog(@"[%@] %@", self.sensorName, error.debugDescription);
}
if (handler != nil) {
handler(self.sensorName, results, start, end, error);
handler(self.sensorName, results, from, to, error);
}
}];
}

- (void)fetchDataFrom:(NSDate *)from to:(NSDate *)to limit:(int)limit all:(bool)all handler:(LimitedDataFetchHandler)handler{
NSManagedObjectContext *private = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[private setParentContext:self.mainQueueManagedObjectContext];
[private performBlock:^{

NSNumber * startNum = [AWAREUtils getUnixTimestamp:from];
NSNumber * endNum = [AWAREUtils getUnixTimestamp:to];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:self->entityName];
[fetchRequest setEntity:[NSEntityDescription entityForName:self->entityName inManagedObjectContext:self.mainQueueManagedObjectContext]];
[fetchRequest setIncludesSubentities:NO];
[fetchRequest setResultType:NSDictionaryResultType];
if([self->entityName isEqualToString:@"EntityESMAnswer"] ){
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"double_esm_user_answer_timestamp >= %@ AND double_esm_user_answer_timestamp =< %@",
startNum, endNum]];
}else{
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"timestamp >= %@ AND timestamp <= %@", startNum, endNum]];
}

[fetchRequest setFetchLimit:limit];

//Set sort option
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

//Get NSManagedObject from managedObjectContext by using fetch setting
NSError * error = nil;
NSArray *results = [private executeFetchRequest:fetchRequest error:&error];

if (error!=nil) {
NSLog(@"[%@] %@", self.sensorName, error.debugDescription);
}
if (handler != nil) {
if (results.count < limit || all) {
handler(self.sensorName, results, from, to, true, error);
}else{
handler(self.sensorName, results, from, to, false, error);
NSDictionary * dict = results.lastObject;
if (dict != nil) {
NSNumber * timestamp = dict[@"timestamp"];
if (timestamp != nil) {
NSDate * date = [NSDate dateWithTimeIntervalSince1970:timestamp.doubleValue/1000];
[self fetchDataFrom:date to:to limit:limit all:all handler:handler];
}else{
handler(self.sensorName, results, from, to, true, error);
}
}else{
handler(self.sensorName, results, from, to, true, error);
}
}
}
}];
}
Expand Down
Loading

0 comments on commit 4bc1a24

Please sign in to comment.