Search code examples
swiftxcodeswift3realmxcode8

Realm RLM Exception when calling Realm()


I am using Realm for swift 3.1 and have imported it via import RealmSwift and import Realm. When I this line is executed in my swift code I get this error let realm = try? Realm() (or ...= try! Realm()... or let r = ...)

>2017-08-16 19:39:53.666 PROJECT_NAME[19996:1183826] *** Terminating app due to uncaught exception 'RLMException', reason: 'Property 'members' is declared as 'NSArray', which is not a supported RLMObject property type. All properties must be primitives, NSString, NSDate, NSData, NSNumber, RLMArray, RLMLinkingObjects, or subclasses of RLMObject. See https://realm.io/docs/objc/latest/api/Classes/RLMObject.html for more information.'
>*** First throw call stack:
>(
>   0   CoreFoundation                      0x000000010978fb0b >__exceptionPreprocess + 171
>   1   libobjc.A.dylib                     0x00000001091f4141 objc_exception_throw + 48
>   2   Realm                               0x000000010851a02e -[RLMProperty setTypeFromRawType:] + 1601
>   3   Realm                               0x000000010851a5dc -[RLMProperty initSwiftPropertyWithName:indexed:linkPropertyDescriptor:property:instance:] + 857
>   4   Realm                               0x000000010850e069 +[RLMObjectSchema propertiesForClass:isSwift:] + 695
>   5   Realm                               0x000000010850cff8 +[RLMObjectSchema schemaForObjectClass:] + 506
>   6   Realm                               0x000000010857d068 _ZL16RLMRegisterClassP10objc_class + 142
>   7   Realm                               0x000000010857d925 __25+[RLMSchema sharedSchema]_block_invoke + 12
>   8   CoreFoundation                      0x00000001097185ff __65-[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:]_block_invoke + 111
>   9   CoreFoundation                      0x00000001097184fa -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 202
>   10  Realm                               0x000000010857d7ac +[RLMSchema sharedSchema] + 142
>   11  Realm                               0x0000000108573d08 +[RLMRealm realmWithConfiguration:error:] + 1148
>   12  RealmSwift                          0x0000000108ba4554 _TFC10RealmSwift5RealmcfzT_S0_ + 100
>   13  PROJECT_NAME                            0x000000010821415e _TFC8PROJECT_NAME19LoginViewController5loginfP_T_ + 1422
>   14  PROJECT_NAME                            0x0000000108215e43 _TToFC8PROJECT_NAME19LoginViewController5loginfP_T_ + 67
>   15  UIKit                               0x000000010a0a6d82 -[UIApplication sendAction:to:from:forEvent:] + 83
>   16  UIKit                               0x000000010a22b5ac -[UIControl sendAction:to:forEvent:] + 67
>   17  UIKit                               0x000000010a22b8c7 -[UIControl _sendActionsForEvents:withEvent:] + 450
>   18  UIKit                               0x000000010a22a802 -[UIControl touchesEnded:withEvent:] + 618
>   19  UIKit                               0x000000010a1147ea -[UIWindow _sendTouchesForEvent:] + 2707
>   20  UIKit                               0x000000010a115f00 -[UIWindow sendEvent:] + 4114
>   21  UIKit                               0x000000010a0c2a84 -[UIApplication sendEvent:] + 352
>   22  UIKit                               0x000000010a8a65d4 >__dispatchPreprocessedEventFromEventQueue + 2926
>   23  UIKit                               0x000000010a89e532 __handleEventQueue >+ 1122
>   24  CoreFoundation                      0x0000000109735c01 >__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
>   25  CoreFoundation                      0x000000010971b0cf >__CFRunLoopDoSources0 + 527
>   26  CoreFoundation                      0x000000010971a5ff __CFRunLoopRun + >911
>   27  CoreFoundation                      0x000000010971a016 >CFRunLoopRunSpecific + 406
>   28  GraphicsServices                    0x00000001113dca24 GSEventRunModal + >62
>   29  UIKit                               0x000000010a0a5134 UIApplicationMain + >159
>   30  PROJECT_NAME                            0x0000000108212a97 main + 55
>   31  libdyld.dylib                       0x000000010cf8465d start + 1
>)
>libc++abi.dylib: terminating with uncaught exception of type NSException
>(lldb) 

I don't believe this has anything to do with my device as my friend/team mate has the same issue with our app. We are both (I believe) using Xcode 8.3.3 and swift 3.1.

Our code does work when we put realm in a testing app and run it. If it is of any relevance we are also using Almofire, although putting this in our test app didn't break anything.

EDIT 1: The only code we have that use's realm at all is the one line above, we haven't actually stored anything via realm

EDIT 2: Above edit is incorrect, we do have code querying for realm objects.

EDIT 3: Have changed it so all code mentioning realm that isn't simply calling Realm() is commented out, issue persists. I.E; EDIT 1 is still accurate


Solution

  • Why don't you read the error message? It clearly states that you are using an unsupported type in one of your Realm models.

    As the error messages says, you cannot have a stored property of type NSArray (since you are using Swift, you might have been trying to store Array<Any>, which is the equivalent of NSArray). If you really need to store a collection of primitives in one of your model classes, as a workaround you could create a custom model class with only one property and store a List of values of that type.

    The error is easy to reproduce using the following model class (I just added a new property to the Car class from the examples):

    class Car: Object {
        dynamic var brand = ""
        dynamic var name: String?
        dynamic var year = 0
        dynamic var array = [Int]()
    
        override var description: String { return "Car {\(brand), \(name), \(year)}" }
    }
    

    The mentioned error occurs on this line, as soon as you try to instantiate Realm, even if you don't create an instance of Car.

    let realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "TemporaryRealm"))