Husein Behboodi Rad Husein Behboodi Rad - 4 months ago 137
iOS Question

didFinishLaunchingWithOptions not called but app is running and faced with crash

I had seen lots of iPhone apps that running on iPad with buttons name

x1
and
x2
. I have an application that first I developed it as a
universal
app but now I changed it to
iPhone
and expect to run it on iPad with that
x1
and
x2
style but when I run it on the iPad I see this core data error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Settings''
*** First throw call stack:
(0x187b6659c 0x1987cc0e4 0x18781d278 0x100110984 0x100110ab8 0x100110494 0x1000f4cdc 0x1000d2644 0x1000d2720 0x18c835544 0x18c318844 0x18c3187b0 0x18c834fbc 0x18c832ce8 0x18c344788 0x18c5af238 0x18c5aecec 0x18c5aec44 0x18c5a2578 0x18c5a1fe0 0x18fdb1edc 0x18fdc162c 0x187b1ea28 0x187b1db30 0x187b1c154 0x187a490a4 0x18c3833c8 0x18c37e3c0 0x100108098 0x198e3aa08)
libc++abi.dylib: terminating with uncaught exception of type NSException


It is working right on iPhone but I see this crash in iPad?

I have this code in my didFinishLaunchingWithOptions that initialize core data

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


//--------- Initialize appearance and database ------------//
[[BRCoreRepository instance] initializeDatabase:self.managedObjectContext];

// Othe ui codes is here

return YES;

}


In iPad version the
didFinishLaunchingWithOptions
it is not calling so the app do not pass the initialize phase of core data and the before showing the viewController it crashes.

Please notice that it is working perfect in iPhone and the problem exist in iPad

This is the stack trace in iPad simulator:

*** First throw call stack:
(
0 CoreFoundation 0x000000010d11dd85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010c280deb objc_exception_throw + 48
2 CoreData 0x000000010cc6448b +[NSEntityDescription entityForName:inManagedObjectContext:] + 251
3 APMB 0x000000010bb8f0e4 -[BRSettingsRepository getAllRowsInSettings] + 100
4 APMB 0x000000010bb8f24b -[BRSettingsRepository loadDataFromDB] + 43
5 APMB 0x000000010bb8ec3c +[BRSettingsRepository instance] + 332
6 APMB 0x000000010bb7333e -[BRLanguageManager language] + 46
7 APMB 0x000000010bb4e084 brAppFontWithSize + 68
8 APMB 0x000000010bb4e145 +[UIFont(SystemFontOverride) systemFontOfSize:] + 37
9 UIKit 0x000000010e5e031d -[UIZoomViewController loadView] + 86
10 UIKit 0x000000010e191560 -[UIViewController loadViewIfRequired] + 138
11 UIKit 0x000000010e191cd3 -[UIViewController view] + 27
12 UIKit 0x000000010e5dfcd0 -[UIZoomViewController init] + 82
13 UIKit 0x000000010e5dd85e -[UIClassicController _setupWindow] + 582
14 UIKit 0x000000010e5dd52b +[UIClassicController sharedClassicController] + 246
15 UIKit 0x000000010e0162b5 -[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:] + 557
16 UIKit 0x000000010e015dde -[UIApplication _handleApplicationLifecycleEventWithScene:transitionContext:completion:] + 508
17 UIKit 0x000000010dff48a0 __70-[UIApplication scene:didUpdateWithDiff:transitionContext:completion:]_block_invoke + 159
18 UIKit 0x000000010dff452d -[UIApplication scene:didUpdateWithDiff:transitionContext:completion:] + 843
19 UIKit 0x000000010dff2bca -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 591
20 FrontBoardServices 0x00000001123d32af __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 265
21 FrontBoardServices 0x00000001123eb8c8 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
22 FrontBoardServices 0x00000001123eb741 -[FBSSerialQueue _performNext] + 178
23 FrontBoardServices 0x00000001123ebaca -[FBSSerialQueue _performNextFromRunLoopSource] + 45
24 CoreFoundation 0x000000010d043301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
25 CoreFoundation 0x000000010d03922c __CFRunLoopDoSources0 + 556
26 CoreFoundation 0x000000010d0386e3 __CFRunLoopRun + 867
27 CoreFoundation 0x000000010d0380f8 CFRunLoopRunSpecific + 488
28 UIKit 0x000000010dff1f21 -[UIApplication _run] + 402
29 UIKit 0x000000010dff6f09 UIApplicationMain + 171
30 APMB 0x000000010bb86e2f main + 111
31 libdyld.dylib 0x00000001108b592d start + 1
32 ??? 0x0000000000000001 0x0 + 1
)


I am wondering how it is possible that
didFinishLaunchingWithOptions
is not calling but the view is going to show. And where is the best place to initialize the core data and initialize methods to do not face this problems??

Answer

Finally I found that it is because of UIFont+SystemFontOverride class.

I use this class to override system font of my application:

#import "UIFont+SystemFontOverride.h"

@implementation UIFont (SystemFontOverride)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"

+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize {
    //return My Custom Bold Font;
}

+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {
    //return My Custom Normal Font;
}

#pragma clang diagnostic pop

@end

it seem that when we are using iPad with ZoomViewController the method are calling before didFinishLaunchingWithOptions and the problem was that In implementation of boldSystemFontOfSize and systemFontOfSize I was using a repository that is initializing at didFinishLaunchingWithOptions