【问题标题】:didFinishLaunchingWithOptions not called but app is running and faced with crash未调用 didFinishLaunchingWithOptions 但应用程序正在运行并面临崩溃
【发布时间】:2016-04-14 06:39:58
【问题描述】:

我见过很多在 iPad 上运行的 iPhone 应用程序,按钮名称为 x1x2。我有一个应用程序,首先我将其开发为universal 应用程序,但现在我将其更改为iPhone,并希望以x1x2 风格在iPad 上运行它,但是当我在iPad 上运行它时,我看到这个核心数据错误:

    *** 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

它在 iPhone 上可以正常工作,但我在 iPad 上看到这个崩溃?

我的 didFinishLaunchingWithOptions 中有这段代码用于初始化核心数据

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


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

// Othe ui codes is here

    return YES;

}

在 iPad 版本中,didFinishLaunchingWithOptions 没有调用,因此应用程序没有通过核心数据的初始化阶段,并且在显示 viewController 之前它崩溃了。

请注意它在 iPhone 上运行良好,而在 iPad 上存在问题

这是 iPad 模拟器中的堆栈跟踪:

    *** 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
)

我想知道didFinishLaunchingWithOptions 怎么可能没有调用但视图会显示。以及初始化核心数据和初始化方法的最佳位置在哪里,不会遇到这个问题??

【问题讨论】:

  • 从您的模拟器或真实设备中删除以前的应用程序并再次清理构建。在核心数据中添加设置对象的位置添加断点。检查哪个键是 nil
  • 这个错误可能与核心数据模型改变有关,当然与通用或非通用无关。
  • 请看我的更新
  • 您将nil 传递给CoreData。在BRSettingsRepository getAllRowsInSettings 中放置一个条件断点以便找到它。
  • 你说得对,我找到了。这是因为 didFinishLaunchingWithOptions 没有在 iPad 中调用。请查看我分享的最近的堆栈跟踪。它正在 iphone 上调用,但它没有在 ipad 上调用,我正在使用该方法初始化存储库

标签: ios objective-c iphone xcode ipad


【解决方案1】:

最后发现是因为UIFont+SystemFontOverride类。

我使用这个类来覆盖我的应用程序的系统字体:

#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

似乎当我们使用带有ZoomViewController 的iPad 时,该方法在didFinishLaunchingWithOptions 之前调用,问题是在boldSystemFontOfSizesystemFontOfSize 的实现中,我使用的是在didFinishLaunchingWithOptions 初始化的存储库

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2015-03-31
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多