全局初始化域导致崩溃

时间:2018-01-04 20:05:45

标签: ios swift realm appdelegate

我有一个快速的应用程序,其中集成了领域。我在AppDelegate.swift中全局声明域,如下面的代码所示。当我执行此应用程序时,我的应用程序在设备上运行正常,甚至在测试模式下运行。

问题:

我的应用程序提交后,Apple审核委员会拒绝了应用程序,符号化的崩溃日志指出了域的初始化。

以下是代码,我可能做错了什么?

import UIKit
import CoreData
import Firebase
import UserNotifications
import Fabric

import RealmSwift

var uiRealm = try! Realm()

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {

var window: UIWindow?
let gcmMessageIDKey = "gcm.message_id"
var tokenExists = false

let imageDb = DigitalFingerprint()
var uid: String!


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


    #if DEVELOPMENT
        print("Development Mode Started")
        let filePath = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist")
        guard let fileopts = FirebaseOptions.init(contentsOfFile: filePath!)
            else {
                fatalError("Couldn't load config file")
        }
        FirebaseApp.configure(options: fileopts)
    #else
        print("Production Mode Started")
        FirebaseApp.configure()
    #endif

    Auth.auth().addStateDidChangeListener { (auth, user) in
        if (user != nil) {
            print("User is there")
            self.uid = KeychainWrapper.standard.string(forKey: KEY_UID)
        } else {
            print("user signed out")
            DB_URL.removeAllObservers()
            DispatchQueue.main.async {
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let vc = storyboard.instantiateViewController(withIdentifier: "starts") as? BootVC
                var topRootViewController: UIViewController = (UIApplication.shared.keyWindow?.rootViewController)!
                while((topRootViewController.presentedViewController) != nil){
                    topRootViewController = topRootViewController.presentedViewController!
                }
                topRootViewController.present(vc!, animated: true, completion: nil)
            }
        }
    }

    if uid != nil {
        checkforAccess()
    }

    return true
}

以下是Real对象,DigitalFingerprint.swift

import Foundation
import RealmSwift
import UIKit

class DigitalFingerprint: Object {

@objc dynamic var imageData: Data? = nil
@objc dynamic var fid : String? = nil
@objc dynamic var uid : String? = nil
override static func primaryKey() -> String {
    return "uid"
}

}

extension DigitalFingerprint {

func writeToRealm(){
    try! uiRealm.write {
        uiRealm.add(self, update: true)
    }
}

func DeleteFromRealm(object: Results<DigitalFingerprint>){
    try! uiRealm.write {
        uiRealm.delete(object)
    }
}

}

崩溃苹果审查委员会收到的日志。 :

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  0

Application Specific Information:
abort() called

Filtered syslog:
None found

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib          0x00000001830142e8 __pthread_kill + 8
1   libsystem_pthread.dylib         0x00000001831292f8 pthread_kill$VARIANT$mp + 396
2   libsystem_c.dylib               0x0000000182f83060 __abort + 152
3   libsystem_c.dylib               0x0000000182f82fc8 __abort + 0
4   libc++abi.dylib                 0x000000018271f068 __cxa_bad_cast + 0
5   libc++abi.dylib                 0x000000018271f1f8 default_terminate_handler+ 8696 () + 280
6   libobjc.A.dylib                 0x0000000182748820 _objc_terminate+ 34848 () + 140
7   SpotMi                          0x0000000100dc7d78 CLSTerminateHandler() + 1260920 (CLSException.mm:314)
8   libc++abi.dylib                 0x000000018273754c std::__terminate(void (*)+ 107852 ()) + 16
9   libc++abi.dylib                 0x00000001827375b8 std::terminate+ 107960 () + 60
10  Realm                           0x0000000101f94a00 realm::Realm::open_with_config(realm::Realm::Config const&, std::__1::unique_ptr<realm::Replication, std::__1::default_delete<realm::Replication> >&, std::__1::unique_ptr<realm::SharedGroup, std::__1::default_delete<realm::SharedGroup> >&, std::__1::unique_ptr<realm::Group, std::__1::default_delete<realm::Group> >&, realm::Realm*) + 985600 (memory:3006)
11  Realm                           0x0000000101f944e0 realm::Realm::Realm(realm::Realm::Config, std::__1::shared_ptr<realm::_impl::RealmCoordinator>) + 984288 (shared_realm.cpp:55)
12  Realm                           0x0000000101ed9658 realm::Realm::make_shared_realm(realm::Realm::Config, std::__1::shared_ptr<realm::_impl::RealmCoordinator>)::make_shared_enabler::make_shared_enabler(realm::Realm::Config, std::__1::shared_ptr<realm::_impl::RealmCoordinator>) + 218712 (shared_realm.hpp:304)
13  Realm                           0x0000000101ed94cc std::__1::shared_ptr<realm::Realm::make_shared_realm(realm::Realm::Config, std::__1::shared_ptr<realm::_impl::RealmCoordinator>)::make_shared_enabler> std::__1::shared_ptr<realm::Realm::make_shared_realm(realm::Realm::Config, std::__1::shared_ptr<realm::_impl::RealmCoordinator>)::make_shared_enabler>::make_shared<realm::Realm::Config, std::__1::shared_ptr<realm::_impl::RealmCoordinator> >(realm::Realm::Config&&,    std::__1::shared_ptr<realm::_impl::RealmCoordinator>&&) + 218316 (memory:2172)
14  Realm                           0x0000000101ed56a4 realm::_impl::RealmCoordinator::get_realm(realm::Realm::Config) + 202404 (realm_coordinator.cpp:229)
15  Realm                           0x0000000101f965f0 realm::Realm::get_shared_realm(realm::Realm::Config) + 992752 (shared_realm.cpp:250)
16  Realm                           0x0000000101f7016c +[RLMRealm realmWithConfiguration:error:] + 835948 (RLMRealm.mm:400)
17  RealmSwift                      0x00000001023911e4 Realm.init() + 152036 (Realm.swift:0)
18  RealmSwift                      0x0000000102391178 Realm.__allocating_init() + 151928 (Realm.swift:0)
19  SpotMi                          0x0000000100c9be14 globalinit_33_E3853139275E4567E078AE3BFE0165A3_func0 + 32276 (AppDelegate.swift:23)
20  libdispatch.dylib               0x0000000182e7ea14 _dispatch_client_callout + 16
21  libdispatch.dylib               0x0000000182e82240 dispatch_once_f$VARIANT$mp + 60
22  SpotMi                          0x0000000100cc2254 FeedVC.viewDidLoad() + 189012 (FeedVC.swift:84)
23  SpotMi                          0x0000000100cc2650 @objc FeedVC.viewDidLoad() + 190032 (FeedVC.swift:0)
24  UIKit                           0x000000018c9e3ae8 -[UIViewController loadViewIfRequired] + 1040
25  UIKit                           0x000000018c9e36c0 -[UIViewController view] + 28
26  UIKit                           0x000000018c9ea28c -[UIWindow    addRootViewControllerViewIfPossible] + 136
27  UIKit                           0x000000018c9e74ec -[UIWindow _setHidden:forced:] + 272
28  UIKit                           0x000000018ca563a0 -[UIWindow makeKeyAndVisible] + 48
29  UIKit                           0x000000018cc43d10 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3620
30  UIKit                           0x000000018cc48aa8 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1712
31  UIKit                           0x000000018cee42d4 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 800
32  UIKit                           0x000000018d1bc12c +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 160
33  UIKit                           0x000000018cee3f38 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 252
34  UIKit                           0x000000018cee4798 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 732
35  UIKit                           0x000000018d6697d0 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 260
36  UIKit                           0x000000018d669674 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 448
37  UIKit                           0x000000018d3d72dc __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 220
38  UIKit                           0x000000018d5703dc _performActionsWithDelayForTransitionContext + 112
39  UIKit                           0x000000018d3d718c -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 252
40  UIKit                           0x000000018d1bb78c -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 364
41  UIKit                           0x000000018cc471ec -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 540
42  UIKit                           0x000000018d05cde8 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 364
43  FrontBoardServices              0x0000000185bc61f0 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 364
44  FrontBoardServices              0x0000000185bceaf8 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 224
45  libdispatch.dylib               0x0000000182e7ea14 _dispatch_client_callout + 16
46  libdispatch.dylib               0x0000000182e86200    _dispatch_block_invoke_direct$VARIANT$mp + 288
47  FrontBoardServices              0x0000000185bfa7f8 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 36
48  FrontBoardServices              0x0000000185bfa49c -[FBSSerialQueue _performNext] + 404
49  FrontBoardServices              0x0000000185bfaa38 -[FBSSerialQueue _performNextFromRunLoopSource] + 56
50  CoreFoundation                  0x00000001834aa97c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
51  CoreFoundation                  0x00000001834aa8fc __CFRunLoopDoSource0 + 88
52  CoreFoundation                  0x00000001834aa184 __CFRunLoopDoSources0 + 204
53  CoreFoundation                  0x00000001834a7d5c __CFRunLoopRun + 1048
54  CoreFoundation                  0x00000001833c7e58 CFRunLoopRunSpecific + 436
55  GraphicsServices                0x0000000185274f84 GSEventRunModal + 100
56  UIKit                           0x000000018ca4767c UIApplicationMain + 236
57  SpotMi                          0x0000000100c99b4c main + 23372 (SurpriseCell.swift:26)
58  libdyld.dylib                   0x0000000182ee456c start + 4

我也在我的initialviewcontroller(Feedvc)中调用此函数,在viewDidAppear()

我的程序流程是

1.load feedvc

  1. 如果用户为零

  2. 转到bootVc(从app delegate调用的那个)

  3. 我究竟做错了什么?

    另外,只是在全局声明一个类型的变量,然后在didFinishLaunching()中初始化变量是一个更好的选择吗?

    谢谢!

1 个答案:

答案 0 :(得分:0)

嗯,在课外创建变量不是一个好主意。尝试创建

var uiRealm = try! Realm()

在AppDelegate类中。并在其他课程中使用(UIApplication.shared.delegate as! AppDelegate).uiRealm

相关问题