[从2016年开始更新旧的领域对象会导致崩溃

问题描述 投票:0回答:1

帮助!我正在从2016年更新一个旧的Xcode项目。使用Realm对象时遇到崩溃。

2016年,Realm属性写为:

    dynamic var numCorrect: Int = 0
    dynamic var numWrong: Int = 0
    dynamic var score: Int = 0
    dynamic var stars: Int = 0
    dynamic var date = NSDate()

[当我尝试使用这些属性创建新对象时,它崩溃并显示以下错误:

Terminating app due to uncaught exception 'RLMException', reason: 'Invalid array input: more values (5) than properties (0).'

好,因此Realm现在需要在“ dynamic var”之前输入“ @objc”。现在是:

    @objc dynamic var numCorrect: Int = 0
    @objc dynamic var numWrong: Int = 0
    @objc dynamic var score: Int = 0
    @objc dynamic var stars: Int = 0
    @objc dynamic var date = NSDate()

当我添加“ @objc”时,没有错误,我的新版本效果很好!但是,当我尝试从App Store中的旧版本更新它时,在尝试创建新对象时会崩溃。我认为可以通过某种方式进行迁移,但我无法弄清楚。

这里是日志:

/Users/realm/workspace/realm_realm-core_release_6.0.6/src/realm/group.hpp:1149: [realm-core-6.0.6] Assertion failed: m_top.size() > s_hist_version_ndx
0   Realm                               0x0000000103b5db04 _ZN5realm4utilL18terminate_internalERNSt3__118basic_stringstreamIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE + 28
1   Realm                               0x0000000103b5dda0 _ZN5realm4util9terminateEPKcS2_lOSt16initializer_listINS0_9PrintableEE + 324
2   Realm                               0x0000000103a97720 _ZN5realm2DB19upgrade_file_formatEbiii + 980
3   Realm                               0x0000000103a95130 _ZN5realm2DB7do_openERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEbbNS_9DBOptionsE + 3092
4   Realm                               0x0000000103a97b20 _ZN5realm2DB4openERNS_11ReplicationENS_9DBOptionsE + 244
5   Realm                               0x0000000103a9c0a4 _ZN5realm2DB6createERNS_11ReplicationENS_9DBOptionsE + 392
6   Realm                               0x0000000103644a44 _ZN5realm5_impl16RealmCoordinator7open_dbEv + 972
7   Realm                               0x0000000103643c98 _ZN5realm5_impl16RealmCoordinator12do_get_realmENS_5Realm6ConfigERNSt3__110shared_ptrIS2_EENS_4util8OptionalINS_9VersionIDEEERNS8_17CheckedUniqueLockE + 72
8   Realm                               0x0000000103643b30 _ZN5realm5_impl16RealmCoordinator9get_realmENS_5Realm6ConfigENS_4util8OptionalINS_9VersionIDEEE + 268
9   Realm                               0x0000000103895ae0 _ZN5realm5Realm16get_shared_realmENS0_6ConfigE + 152
10  Realm                               0x00000001037e0434 +[RLMRealm realmWithConfiguration:queue:error:] + 1704
11  RealmSwift                          0x0000000104be1660 $sSo8RLMRealmC13configuration5queueABSo0A13ConfigurationC_So012OS_dispatch_C0CSgtKcfCTO + 156
12  RealmSwift                          0x0000000104c18aa8 $s10RealmSwift0A0V5queueACSo012OS_dispatch_C0CSg_tKcfC + 140
13  Scrath                              0x00000001028afa4c $s6Scrath11AppDelegateC11updateRealmyyF + 680
14  Scrath                              0x00000001028af438 $s6Scrath11AppDelegateC11application_29didFinishLaunchingWithOptionsSbSo13UIApplicationC_SDySo0j6LaunchI3KeyaypGSgtF + 288
15  Scrath                              0x00000001028af534 $s6Scrath11AppDelegateC11application_29didFinishLaunchingWithOptionsSbSo13UIApplicationC_SDySo0j6LaunchI3KeyaypGSgtFTo + 232
16  UIKitCore                           0x00000001c31e478c 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 10893196
17  UIKitCore                           0x00000001c31e656c 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 10900844
18  UIKitCore                           0x00000001c31ebf68 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 10923880
19  UIKitCore                           0x00000001c295eb50 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 1956688
20  UIKitCore                           0x00000001c2e29510 _UIScenePerformActionsWithLifecycleActionMask + 112
21  UIKitCore                           0x00000001c295f684 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 1959556
22  UIKitCore                           0x00000001c295f0b0 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 1958064
23  UIKitCore                           0x00000001c295f4a0 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 1959072
24  UIKitCore                           0x00000001c295ed28 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 1957160
25  UIKitCore                           0x00000001c2963444 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 1975364
26  UIKitCore                           0x00000001c2d48080 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 6058112
27  UIKitCore                           0x00000001c2e431b4 _UISceneSettingsDiffActionPerformChangesWithTransitionContext + 260
28  UIKitCore                           0x00000001c2963180 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 1974656
29  UIKitCore                           0x00000001c2e4309c _UISceneSettingsDiffActionPerformActionsWithDelayForTransitionContext + 108
30  UIKitCore                           0x00000001c2962fd8 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 1974232
31  UIKitCore                           0x00000001c27c8db8 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 294328
32  UIKitCore                           0x00000001c27c78b8 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 288952
33  UIKitCore                           0x00000001c27c8ae8 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 293608
34  UIKitCore                           0x00000001c31ea200 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 10916352
35  UIKitCore                           0x00000001c2d6ea50 66C0BDEB-71CF-3148-AF27-A5B055FAD9A5 + 6216272
36  FrontBoardServices                  0x00000001c4304bec 039FCB83-A7EC-3872-94E8-DD490434EAEB + 52204
37  FrontBoardServices                  0x00000001c432b190 039FCB83-A7EC-3872-94E8-DD490434EAEB + 209296
38  FrontBoardServices                  0x00000001c430faac 039FCB83-A7EC-3872-94E8-DD490434EAEB + 96940
39  FrontBoardServices                  0x00000001c432ae24 039FCB83-A7EC-3872-94E8-DD490434EAEB + 208420
40  libdispatch.dylib                   0x0000000104da318c _dispatch_client_callout + 20
41  libdispatch.dylib                   0x0000000104da6964 _dispatch_block_invoke_direct + 368
42  FrontBoardServices                  0x00000001c43512b4 039FCB83-A7EC-3872-94E8-DD490434EAEB + 365236
43  FrontBoardServices                  0x00000001c4350f60 039FCB83-A7EC-3872-94E8-DD490434EAEB + 364384
44  FrontBoardServices                  0x00000001c43514cc 039FCB83-A7EC-3872-94E8-DD490434EAEB + 365772
45  CoreFoundation                      0x00000001bf01f860 9624AAFD-5437-3772-A507-0F357875808D + 710752
46  CoreFoundation                      0x00000001bf01f7b4 9624AAFD-5437-3772-A507-0F357875808D + 710580
47  CoreFoundation                      0x00000001bf01ef04 9624AAFD-5437-3772-A507-0F357875808D + 708356
48  CoreFoundation                      0x00000001bf019ca4 9624AAFD-5437-3772-A507-0F357875808D + 687268
49  CoreFoundation                      0x00000001bf019660 CFRunLoopRunSpecific + 480
50  GraphicsServices                    0x00000001c942a604 GSEventRunModal + 164
51  UIKitCore                           0x00000001c31ee15c UIApplicationMain + 1944
52  Scrath                              0x00000001028b16a4 main + 80
53  libdyld.dylib                       0x00000001bee951ec 95B366E7-F5BD-3308-9416-24B35999029B + 4588!!! IMPORTANT: Please send this log and info about Realm SDK version and other relevant reproduction info to [email protected]

请,有人知道如何解决吗?这并不是说该应用程序很受欢迎,但我不想要求任何以前的用户删除该应用程序才能使用新版本。

xcode realm
1个回答
0
投票

我将尝试实现一个迁移块,并按如下所示使用枚举:

migration.enumerateObjects(ofType: Object.className()) { (oldObject, newObject) in
    guard let numCorrect = oldObject?["numCorrect"] as? Int else {
        fatalError("numCorrect is not a Int")
    }
    // List all the rest of your properties the same way (i.e., with a guard statement)

    // Then assign old object value to new property
    newObject?["numCorrect"] = numCorrect

}

确保使用正确的版本等正确实现迁移块。

© www.soinside.com 2019 - 2024. All rights reserved.