1

I am having an unexpected issue with a crash on my app which I am specifically struggling to debug because it occurs in background at a time determined by the iOS system. I have some capitalised comments to the code which show where the issue is being back traced to. I hope this is clear.

I believe it has to do with object deallocation.

  • I have tried using the __block before initialising the object but this has not helped.
  • I have also tried dispatching the lines of code in error to the main queue but that has not helped.

The actual crash is listed as AppName: __66-[BackgroundUpdateController initiateBackgroundHealthkitObservers]_block_invoke_5 + 160

I apologise if some of the code does not fit standard formatting and conventions. I am self taught from a variety of places and so do not have proper experience with code format.

Many Thanks

#import "BackgroundUpdateController.h"
NSUserDefaults *backgroundDefaults;
@implementation BackgroundUpdateController

-(id)init{
backgroundDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.HeartAnalyzer"];
return [super init];
}

-(void)initiateBackgroundHealthkitObservers{
// Check we should be running here
if(([backgroundDefaults integerForKey:@"sleepAnalysisEnabled"] != 1) || (![backgroundDefaults boolForKey:@"AutomaticSleepAdd"])) return;
// Initiate some variables, Use __block to ensure the backgroundHealthStore object does not get deallocated
__block HKHealthStore *backgroundHealthStore = [[HKHealthStore alloc] init];
HKQuantityType *activeEnergy = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];
// Enable background delivery of active energy data from HealthKit
[backgroundHealthStore enableBackgroundDeliveryForType:activeEnergy frequency:HKUpdateFrequencyHourly withCompletion:^(BOOL success, NSError *error) {
}];
// Now setup an HKOberverQuery which triggers hourly if there are new active energy data points in HealthKit
HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:activeEnergy predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {
    UIApplicationState state = [[UIApplication sharedApplication] applicationState];
    if (state == UIApplicationStateBackground || state == UIApplicationStateInactive){// Only run when app is not in foreground
        // Load some more variables with checks to ensure they are valid objects
        NSDate *previousSavedDate = [backgroundDefaults objectForKey:@"DateBackgroundSleepLastSaved"];
        if(previousSavedDate == nil) previousSavedDate = [NSDate distantPast];
        NSDate *lastSleepCheck = [backgroundDefaults objectForKey:@"LastSleepCheck"];
        if(lastSleepCheck == nil) lastSleepCheck = [NSDate distantPast];
        // If the last save date was long enough ago and the last sleep check was long enough ago, proceed
        if(([previousSavedDate timeIntervalSinceNow] < -(3600*18)) && ([lastSleepCheck timeIntervalSinceNow] < -(3600*2))){
            [backgroundDefaults setObject:[NSDate date] forKey:@"LastSleepCheck"];
            [backgroundDefaults setBool:NO forKey:@"BackgroundSleepFound"];
            SleepTimesCalculator *sleepClass = [[SleepTimesCalculator alloc] init];
            [sleepClass calculateSleepTimes:^{
                NSLog(@"Background sleep time calculations complete");
                if([backgroundDefaults boolForKey:@"BackgroundSleepFound"]){// Only continue is a sleep time was found
                    __block NSMutableArray *savedSleepObjects = [backgroundDefaults valueForKey:@"SleepTimesDataBase"];
                    if(savedSleepObjects.count > 0){
                            __block NSMutableDictionary *sleepObject = [savedSleepObjects objectAtIndex:0]; // THE __BLOCK USED TO PREVENT THE OBJECT BEING DEALLOCATED, STILL SEEMS TO BE BASED ON THE CRASH
                            NSDate *sleepStart = [NSDate dateWithTimeIntervalSinceReferenceDate:[[sleepObject valueForKey:@"CalculatedSleepTime"]integerValue]];// Get the sleep time start date object
                            NSDate *sleepEnd = [NSDate dateWithTimeIntervalSinceReferenceDate:[[sleepObject valueForKey:@"CalculatedWakeTime"]integerValue]];
                            NSInteger sleepSavedToHealth = [[sleepObject valueForKey:@"SavedToHealth"] integerValue];// Check its not already been saved by some other element of the app
                            if(sleepSavedToHealth != 1){
                                HKCategorySample *sleepSample = [HKCategorySample categorySampleWithType:[HKCategoryType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis] value:1 startDate:sleepStart endDate:sleepEnd];// Generate sleep object for HealthKit
                                [backgroundHealthStore saveObject:sleepSample withCompletion:^(BOOL success, NSError *error) {
                                    if (!success) NSLog(@"Uncommon Error! saveObject:sleepSample");
                                    else{
                                        dispatch_async(dispatch_get_main_queue(), ^{// DISPATCH TO MAIN QUEUE AN ATTEMPTED FIX FOR CRASH
                                            sleepObject = [savedSleepObjects objectAtIndex:0];// Choose the most recent sleep time to save
                                            [sleepObject setValue:[NSNumber numberWithInteger:1] forKey:@"SavedToHealth"];// THIS IS WHERE THE 'Last Exception Backtrace (0)' ENDS UP
                                            [savedSleepObjects replaceObjectAtIndex:0 withObject:sleepObject];// Replace the object which now has the 'Saved' tag
                                            [backgroundDefaults setObject:[NSDate date] forKey:@"DateBackgroundSleepLastSaved"];// Save the data of the last time we reached this point
                                            [backgroundDefaults setObject:savedSleepObjects forKey:@"SleepTimesDataBase"];// Save the sleep times back to the database
                                        });
                                    }
                                }];
                            }
                        completionHandler();// Call the completion handler as we've been throught the sleepObjects array
                    }
                    else completionHandler();// Call the completion handler anyway
                }
                else completionHandler();// Call the completion handler anyway
            }];

        }
        else completionHandler();
    }
}];
[backgroundHealthStore executeQuery:query];// Execute the HealthKit healthstore query
}

@end
Simon
  • 304
  • 2
  • 17
  • `__BLOCK USED TO PREVENT THE OBJECT BEING DEALLOCATED` Can you explain your reasoning? I think this is a big misinterpretation. – ystack Feb 15 '17 at 22:12

1 Answers1

0

Prefixing __block does not guarantees existence of an object for @"CalculatedSleepTime" key in sleepObject

I think you have misinterpreted how __block works. This will be a great guide.

On a quick overview of the code, it seems like [sleepObject valueForKey:@"CalculatedSleepTime"] is returning nil & without a nullability check you are trying to extract the integerValue

So, consider:

NSMutableDictionary *sleepObject = [savedSleepObjects objectAtIndex:0];
id calculatedSleepTime = [sleepObject valueForKey:@"CalculatedSleepTime"];

if(calculatedSleepTime){
    NSDate *sleepStart = [NSDate dateWithTimeIntervalSinceReferenceDate:[calculatedSleepTime integerValue]];
}

And it looks like you also don't require the __block prefix in HKHealthStore *backgroundHealthStore = [[HKHealthStore alloc] init];

Community
  • 1
  • 1
ystack
  • 1,785
  • 12
  • 23