realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.32k stars 2.15k forks source link

RLMException, reason: 'Object type 'Object' not persisted in Realm' #2267

Closed edwardIshaq closed 9 years ago

edwardIshaq commented 9 years ago

Hi,

I have a similar issue to #1916 where I'm trying to create a dataSource to work with different Object subclasses. This worked fine before on the Realm but started getting the same error log as #1916 once i converted the code to RealmSwift

class DataSource<T: AnyObject where T: Hashable>: NSObject {
    var elements = [T]()
    var selectedElements = Set<T>()  
}

class ABContactDS<T: Contact>: DataSource<Object> {
    override init() {
        super.init()
        cacheAllObjects()
    }
    func cacheAllObjects() -> [T] {
        self.elements = Realm().objects(T.self).array
        return self.elements 
    }
}

Any idea of a workaround this issue ?

2015-07-21 11:22:33.998 Pop[82119:6689465] *** Terminating app due to uncaught exception 'RLMException', reason: 'Object type 'Object' not persisted in Realm'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010b046c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010a987bb7 objc_exception_throw + 45
    2   Realm                               0x00000001097ce7fb -[RLMSchema objectForKeyedSubscript:] + 136
    3   Realm                               0x00000001097a3aec RLMGetObjects + 146
    4   RealmSwift                          0x0000000109a1cc0b _TFC10RealmSwift5Realm7objectsfS0_U__FMQ_GCS_7ResultsQ__ + 139
    5   Pop                                 0x0000000109302e9e _TZFE3PopC10RealmSwift6Object10allObjectsfMS1_FT_GSaS1__ + 78
    6   Pop                                 0x0000000109302f19 _TToZFE3PopC10RealmSwift6Object10allObjectsfMS1_FT_GSaS1__ + 25
    7   Pop                                 0x000000010923ffa1 _TFC3Pop11ABContactDS15cacheAllObjectsU__fGS0_Q__FT_GSaQ__ + 65
    8   Pop                                 0x000000010923f782 _TFC3Pop11ABContactDScU__fMGS0_Q__FT_GS0_Q__ + 98
    9   Pop                                 0x000000010923f7ea _TFC3Pop11ABContactDSCU__fMGS0_Q__FT_GS0_Q__ + 74
    10  Pop                                 0x00000001092d5a02 _TFC3Pop17ContactsViewModelcfMS0_FT16googleUIDelegatePSo19GIDSignInUIDelegate__S0_ + 162
    11  Pop                                 0x00000001092d5b7c _TFC3Pop17ContactsViewModelCfMS0_FT16googleUIDelegatePSo19GIDSignInUIDelegate__S0_ + 60
    12  Pop                                 0x00000001092e10bf _TFIvC3Pop22ContactsViewController13lazyViewModelGC12PopFramework7LazyVarCS_17ContactsViewModelPSo19GIDSignInUIDelegate__iU_FPS4__S3_ + 47
    13  Pop                                 0x00000001092e10fe _TTRXFo_oPSo19GIDSignInUIDelegate__oC3Pop17ContactsViewModel_XFo_iPS___iS1__ + 30
    14  Pop                                 0x00000001092dead1 _TPA__TTRXFo_oPSo19GIDSignInUIDelegate__oC3Pop17ContactsViewModel_XFo_iPS___iS1__58 + 81
    15  PopFramework                        0x0000000109d69288 _TFC12PopFramework7LazyVar9calcValueU___fGS0_Q_Q0__FQ0_Q_ + 680
    16  Pop                                 0x00000001092d7e9b _TFC3Pop22ContactsViewControllerg9viewModelCS_17ContactsViewModel + 107
    17  Pop                                 0x00000001092d9148 _TFC3Pop22ContactsViewController13bindViewModelfS0_FT_T_ + 920
    18  Pop                                 0x00000001092d83ea _TFC3Pop22ContactsViewController11viewDidLoadfS0_FT_T_ + 122
    19  Pop                                 0x00000001092d8452 _TToFC3Pop22ContactsViewController11viewDidLoadfS0_FT_T_ + 34
    20  UIKit                               0x000000010c1231d0 -[UIViewController loadViewIfRequired] + 738
    21  UIKit                               0x000000010c1233ce -[UIViewController view] + 27
    22  UIKit                               0x000000010c03e289 -[UIWindow addRootViewControllerViewIfPossible] + 58
    23  UIKit                               0x000000010c03e64f -[UIWindow _setHidden:forced:] + 247
    24  UIKit                               0x000000011cff7695 -[UIWindowAccessibility _orderFrontWithoutMakingKey] + 68
    25  UIKit                               0x000000010c04ade1 -[UIWindow makeKeyAndVisible] + 42
    26  UIKit                               0x000000010bfee417 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
    27  UIKit                               0x000000010bff119e -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
    28  UIKit                               0x000000010bff0095 -[UIApplication workspaceDidEndTransaction:] + 179
    29  FrontBoardServices                  0x0000000110efe5e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
    30  CoreFoundation                      0x000000010af7a41c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    31  CoreFoundation                      0x000000010af70165 __CFRunLoopDoBlocks + 341
    32  CoreFoundation                      0x000000010af6f947 __CFRunLoopRun + 887
    33  CoreFoundation                      0x000000010af6f366 CFRunLoopRunSpecific + 470
    34  UIKit                               0x000000010bfefb02 -[UIApplication _run] + 413
    35  UIKit                               0x000000010bff28c0 UIApplicationMain + 1282
    36  Pop                                 0x0000000109326b97 main + 135
    37  libdyld.dylib                       0x000000010e446145 start + 1
    38  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
segiddins commented 9 years ago

@edwardIshaq what version of Swift are you using? That works fine for me in Swift 2.0 beta 4

edwardIshaq commented 9 years ago

Im on swift 1.2 unfortunately :(

edwardIshaq commented 9 years ago

My current patch just to make it compile and work for the time being :

    func cacheAllObjects() -> [Object] {
        let cache: [T] = Realm().objects(Contact.self).toArray() as! [T]
        self.elements = cache
        return self.elements
    }
edwardIshaq commented 9 years ago

I'm pushing my app via TestFlight, If i move to swift 2.0 I'll probably will lose that option, no ?

segiddins commented 9 years ago

Hm, in that case could you share a project with us, either here or privately at help@realm.io, that demonstrates the issue so we can investigate? Thanks!

edwardIshaq commented 9 years ago

Thanks Sam, I'll prepare it and send it over.

segiddins commented 9 years ago

The root cause of the issue is subclassing DataSource<Object> -- if that's changed to DataSource<T>, everything should work as expected!

edwardIshaq commented 9 years ago

Sweet :+1: Thanks Sam