Skip to content

Commit

Permalink
optimize start when no ui and update in background from notification …
Browse files Browse the repository at this point in the history
…seem to work #91
  • Loading branch information
roznet committed Apr 11, 2021
1 parent 6231e6f commit a563263
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 29 deletions.
2 changes: 1 addition & 1 deletion ConnectStats.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -6948,7 +6948,7 @@
repositoryURL = "https://github.com/roznet/rzexternal";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.2;
minimumVersion = 1.0.3;
};
};
422DCB6D258A2779000DEE4B /* XCRemoteSwiftPackageReference "rzutils" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
"repositoryURL": "https://github.com/roznet/rzexternal",
"state": {
"branch": null,
"revision": "13707fe878e88d0dec93bb2b654bca8cadd271bf",
"version": "1.0.2"
"revision": "8196de67f9481c7f2a1ba2d5bf7886fe89f63ce4",
"version": "1.0.3"
}
},
{
Expand Down
2 changes: 2 additions & 0 deletions ConnectStats/src/GCAppDelegate+Swift.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NS
self.web.notificationHandler = nil;
completionHandler(UIBackgroundFetchResultNoData);
}
// Don't keep startup file
[RZFileOrganizer removeEditableFile:GC_STARTING_FILE];
}


Expand Down
5 changes: 5 additions & 0 deletions ConnectStats/src/GCAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[self registerForPushNotifications];
[self remoteStatusCheck];

/* FOR TESTING
[self application:application didReceiveRemoteNotification:@{} fetchCompletionHandler:^(UIBackgroundFetchResult res){
RZLog(RZLogInfo, @"completed with %@", @(res));
}];
*/
return YES;
}

Expand Down
2 changes: 2 additions & 0 deletions ConnectStats/src/GCConnectStatsRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ -(void)checkToken{
self.oauthToken = nil;
self.userId = 0;
self.tokenId = 0;
}else{
RZLog(RZLogInfo,@"token there");
}
}

Expand Down
6 changes: 6 additions & 0 deletions ConnectStats/src/GCDerivedOrganizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ typedef void (^GCDerivedDidCompleteBestMatchingSeriesBlock)(NSArray<GCDerivedDat
-(GCDerivedOrganizer*)initWithDb:(FMDatabase*)aDb andThread:(dispatch_queue_t)thread;
-(GCDerivedOrganizer*)initForTestModeWithDb:(FMDatabase*)aDb thread:(dispatch_queue_t)thread andFilePrefix:(NSString*)filePrefix;

/**
* call this function when details should be loaded
* typically when the ui is ready, it can be called multiple time
* @return true if details already loaded, false if this actually triggered the load
*/
-(BOOL)ensureDetailsLoaded;
-(FMDatabase*)deriveddb;

/// Return time serie of best rolling series for field for all the calculated dates
Expand Down
48 changes: 32 additions & 16 deletions ConnectStats/src/GCDerivedOrganizer.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ @interface GCDerivedOrganizer ()
@property (nonatomic,retain) NSMutableDictionary<NSDictionary*,GCStatsDataSerie*> * historicalSeriesByKeys;

@property (nonatomic, retain) NSString * useDerivedFilePrefix;
@property (nonatomic, assign) BOOL loadDetailsNeeded;
@property (nonatomic, assign) BOOL loadDetailsCompleted;
@end

@implementation GCDerivedOrganizer
Expand All @@ -118,17 +120,6 @@ -(GCDerivedOrganizer*)initWithDb:(FMDatabase*)aDb thread:(dispatch_queue_t)threa
[self.web attach:self];
self.useDerivedFilePrefix = filePrefix;

if (thread==nil) {
if( aDb ){
[self loadFromDb];
}else{
self.derivedSeries= [NSMutableDictionary dictionary];
}
}else{
dispatch_async(self.worker,^(){
[self loadFromDb];
});
}
}
return self;
}
Expand All @@ -137,7 +128,9 @@ -(GCDerivedOrganizer*)initWithDb:(FMDatabase*)aDb andThread:(dispatch_queue_t)th
return [self initWithDb:aDb thread:thread andFilePrefix:nil];
}
-(GCDerivedOrganizer*)initForTestModeWithDb:(FMDatabase*)aDb thread:(dispatch_queue_t)thread andFilePrefix:(NSString*)filePrefix{
return [self initWithDb:aDb thread:thread andFilePrefix:filePrefix];
GCDerivedOrganizer * rv = [self initWithDb:aDb thread:thread andFilePrefix:filePrefix];
[rv ensureDetailsLoaded];
return rv;
}

-(void)dealloc{
Expand All @@ -155,6 +148,29 @@ -(void)dealloc{
[super dealloc];
}

-(BOOL)ensureDetailsLoaded{
@synchronized (self) {
self.loadDetailsNeeded = true;
if( self.loadDetailsCompleted ){
return true;
}
}
if (self.worker==nil) {
if( self.db ){
[self loadFromDb];
}else{
self.derivedSeries= [NSMutableDictionary dictionary];
}
return true;
}else{
dispatch_async(self.worker,^(){
[self loadFromDb];
});
return false;
}
}


#pragma mark - Load Derived Series

-(NSString*)derivedFilePrefix{
Expand All @@ -165,6 +181,7 @@ -(void)loadFromDb{
self.derivedSeries = [NSMutableDictionary dictionaryWithCapacity:10];

if ( kDerivedEnabled) {
RZPerformance * perf = [RZPerformance start];
FMDatabase * db = [self deriveddb];
if( db ){
FMResultSet * res= [db executeQuery:@"SELECT * FROM gc_derived_series"];
Expand All @@ -191,20 +208,21 @@ -(void)loadFromDb{
}

if( self.seriesByKeys.count > 0){
RZLog(RZLogInfo, @"Loaded %d derived series and %@ series by key", (int)self.derivedSeries.count, @(self.seriesByKeys.count));
RZLog(RZLogInfo, @"Loaded %d derived series and %@ series by key %@", (int)self.derivedSeries.count, @(self.seriesByKeys.count), perf);
}else{
RZLog(RZLogInfo, @"Loaded %d derived series", (int)self.derivedSeries.count);
}
}
}
self.loadDetailsCompleted = true;
}

-(void)loadHistoricalFileSeries{
BOOL convert = ! [[self deriveddb] tableExists:@"gc_converted_historical_second"];

GCStatsDatabase * statsDb = [GCStatsDatabase database:[self deriveddb] table:@"gc_converted_historical_second"];
RZPerformance * perf = [RZPerformance start];
if( convert ){
RZPerformance * perf = [RZPerformance start];
for (NSString * key in self.derivedSeries) {
GCDerivedDataSerie * serie = self.derivedSeries[key];
if( serie.derivedPeriod == gcDerivedPeriodMonth){
Expand All @@ -224,10 +242,8 @@ -(void)loadHistoricalFileSeries{
}
}
RZLog(RZLogInfo, @"Converted all in %@", perf);
[perf reset];
}
self.historicalSeriesByKeys = [NSMutableDictionary dictionaryWithDictionary:[statsDb loadByKeys]];
RZLog(RZLogInfo, @"Loaded all db in %@", perf);
}

-(BOOL)debugCheckSerie:(GCStatsDataSerie*)serie{
Expand Down
8 changes: 8 additions & 0 deletions ConnectStats/src/GCHealthOrganizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@

-(GCHealthOrganizer*)initForTest NS_DESIGNATED_INITIALIZER;

/**
* call this function when details should be loaded
* typically when the ui is ready, it can be called multiple time
* @return true if details already loaded, false if this actually triggered the load
*/
-(BOOL)ensureDetailsLoaded;


+(void)ensureDbStructure:(FMDatabase*)db;

-(BOOL)addHealthMeasure:(GCHealthMeasure*)one;
Expand Down
34 changes: 25 additions & 9 deletions ConnectStats/src/GCHealthOrganizer.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

@interface GCHealthOrganizer ()


@property (nonatomic,assign) BOOL loadDetailsCompleted;
@end

@implementation GCHealthOrganizer
Expand Down Expand Up @@ -63,13 +63,6 @@ -(GCHealthOrganizer*)initWithDb:(FMDatabase*)db andThread:(dispatch_queue_t)thre
if (self) {
self.db = db;
self.worker = thread;
if( thread ){
dispatch_async(thread,^(){
[self loadFromDb];
});
}else{
[self loadFromDb];
}
}
return self;
}
Expand All @@ -85,6 +78,25 @@ -(GCHealthOrganizer*)initForTest{
}
return self;
}

-(BOOL)ensureDetailsLoaded{
@synchronized (self) {
if( self.loadDetailsCompleted ){
return true;
}
}
if( self.worker ){
dispatch_async(self.worker,^(){
[self loadFromDb];
});
return false;
}else{
[self loadFromDb];
}
return true;

}

-(void)updateForNewProfile{
self.db = [GCAppGlobal db];
if (self.worker) {
Expand All @@ -103,6 +115,7 @@ -(void)clearAllMeasures{
}
}
-(void)loadFromDb{
RZPerformance * perf = [RZPerformance start];
if( self.db == nil){
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
[self addDefaultZoneCalculatorTo:dict];
Expand Down Expand Up @@ -131,7 +144,6 @@ -(void)loadFromDb{
}
}
}
RZLog(RZLogInfo,@"Loaded %lu health measures (%@ types)", (unsigned long)n, @(summary.count));
self.measures = [NSArray arrayWithArray:meas];

NSMutableDictionary * zon = [NSMutableDictionary dictionaryWithCapacity:5];
Expand Down Expand Up @@ -168,6 +180,10 @@ -(void)loadFromDb{
}
self.sleepBlocks = [blocks sortedArrayUsingSelector:@selector(compare:)];

RZLog(RZLogInfo,@"Loaded %lu health measures (%@ types) %@", (unsigned long)n, @(summary.count), perf);
@synchronized (self) {
self.loadDetailsCompleted = true;
}
}

-(BOOL)hasHealthData{
Expand Down
10 changes: 9 additions & 1 deletion ConnectStats/src/GCSplitViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,19 @@ - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibB

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[GCAppGlobal startSuccessful];

dispatch_async([GCAppGlobal worker], ^(){
[[GCAppGlobal organizer] ensureDetailsLoaded];
});
dispatch_async([GCAppGlobal worker], ^(){
[[GCAppGlobal derived] ensureDetailsLoaded];
});

dispatch_async([GCAppGlobal worker], ^(){
[[GCAppGlobal health] ensureDetailsLoaded];
});

[GCAppGlobal startSuccessful];
}
- (void)viewDidLoad
{
Expand Down
4 changes: 4 additions & 0 deletions ConnectStats/src/GCStatsMultiFieldViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ -(void)viewWillAppear:(BOOL)animated{
dispatch_async([GCAppGlobal worker], ^(){
[[GCAppGlobal organizer] ensureDetailsLoaded];
});

dispatch_async([GCAppGlobal worker], ^(){
[[GCAppGlobal derived] ensureDetailsLoaded];
});
}
- (void)didReceiveMemoryWarning
{
Expand Down
9 changes: 9 additions & 0 deletions ConnectStats/src/GCTabBarController.m
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,19 @@ -(void)loadView{
-(void)viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];

dispatch_async([GCAppGlobal worker], ^(){
[[GCAppGlobal organizer] ensureDetailsLoaded];
});

dispatch_async([GCAppGlobal worker], ^(){
[[GCAppGlobal derived] ensureDetailsLoaded];
});

dispatch_async([GCAppGlobal worker], ^(){
[[GCAppGlobal health] ensureDetailsLoaded];
});

[GCAppGlobal startSuccessful];

}
Expand Down

0 comments on commit a563263

Please sign in to comment.