realm / realm-swift

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

Object type 'Test' is not managed by the Realm. #4811

Closed bontoJR closed 7 years ago

bontoJR commented 7 years ago

Goals

I want to retrive objects created using dynamic Realm.

I am creating a test class using the dynamic features like this:

RLMSchema *schema = [[RLMSchema alloc] init];
RLMProperty *prop = [[RLMProperty alloc] initWithName:@"property"
                                                                 type:RLMPropertyTypeString
                                                      objectClassName:nil
                                               linkOriginPropertyName:nil
                                                              indexed:NO
                                                             optional:NO];
RLMObjectSchema *objectSchema = [[RLMObjectSchema alloc] initWithClassName:@"Test"
                                                                           objectClass:RLMObject.class
                                                                            properties:@[prop]];
schema.objects = @[objectSchema];
[[RLMRealm defaultRealm] setSchema:schema];

Then I later try to get the objects out of the Realm with:

[[RLMRealm defaultRealm] objects:objectSchema.className where:@"property = 'test'"];

and I get back the following error:

*** Terminating app due to uncaught exception 'RLMException', reason: 'Object type 'Test' is not managed by the Realm. If using a custom `objectClasses` / `objectTypes` array in your configuration, add `Test` to the list of `objectClasses` / `objectTypes`.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010b859d4b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010b2bb21e objc_exception_throw + 48
    2   Realm                               0x000000010a77f3cc _ZN13RLMSchemaInfoixEP8NSString + 140
    3   Realm                               0x000000010a790eb4 RLMGetObjects + 103
    4   Realm                               0x000000010a7fe89f -[RLMRealm objects:withPredicate:] + 42
    5   Realm                               0x000000010a7fe825 -[RLMRealm objects:where:args:] + 91
    6   Realm                               0x000000010a7fe77e -[RLMRealm objects:where:] + 177
    7   TSOffline                           0x000000010a67d261 -[TSDataManager performSynchronizationWithCallback:] + 1185
    8   Offline                             0x000000010a6d9fa9 -[OFFOfflineManager initWithContainerController:pluginContext:identifier:params:] + 4121
    9   Offline                             0x000000010a6d8d87 -[OFFOfflineModuleDelegate newObjectWithPluginContext:identifier:params:callback:] + 231
    10  Container                           0x000000010a5d5dcd -[TSCAbstractContainerModuleDelegate activate:contentReadyHandler:] + 1181
    11  MAF                                 0x000000010a2eaa25 _TPAo.14 + 53
    12  MAF                                 0x000000010a2ea5f0 _TTRXFo_dC3MAF13PluginContextdXFdCb_dGSqPs9AnyObject______XFo_oS0_oXFo_oGSqPS1_______ + 112
    13  MAF                                 0x000000010a2ea6e2 _TTRXFo_oC3MAF13PluginContextoXFo_oGSqPs9AnyObject______XFo_iTS0_19contentReadyHandlerXFo_iGSqPS1____iT____iT__ + 130
    14  MAF                                 0x000000010a2eab71 _TPA__TTRXFo_oC3MAF13PluginContextoXFo_oGSqPs9AnyObject______XFo_iTS0_19contentReadyHandlerXFo_iGSqPS1____iT____iT__ + 81
    15  MAF                                 0x000000010a2e9d38 _TFC3MAF21PluginDelegateWrapper6pluginfTTS_6Plugin15didActivateWithTS_13PluginContext17completionHandlerGSqFGSqPs9AnyObject__T___T_ + 7112
    16  MAF                                 0x000000010a2eb441 _TToFC3MAF21PluginDelegateWrapper6pluginfTTS_6Plugin15didActivateWithTS_13PluginContext17completionHandlerGSqFGSqPs9AnyObject__T___T_ + 273
    17  MAF                                 0x000000010a2febcf _TFC3MAF6Plugin8activatefzT9argumentsGSqGVs10DictionaryVs11AnyHashableP___17completionHandlerGSqFGSqP__T___T_ + 2287
    18  MAF                                 0x000000010a28153c _TFC3MAF6Plugin11oldActivatefT9argumentsGSqGVs10DictionaryVs11AnyHashableP___17completionHandlerGSqFGSqP__T___T_ + 316
    19  MAF                                 0x000000010a2811b3 _TFC3MAF6Plugin21activateWithArgumentsfTGSqGVs10DictionaryVs11AnyHashableP___19contentReadyHandlerGSqFGSqP__T___T_ + 179
    20  MAF                                 0x000000010a281b96 _TToFC3MAF6Plugin21activateWithArgumentsfTGSqGVs10DictionaryVs11AnyHashableP___19contentReadyHandlerGSqFGSqP__T___T_ + 358
    21  Container                           0x000000010a5c7af6 -[TSCContainerController activate:] + 2102
    22  Container                           0x000000010a5ce216 __45-[TSCContainerController addContainerMethods]_block_invoke.541 + 86
    23  CoreFoundation                      0x000000010b7e044c __invoking___ + 140
    24  CoreFoundation                      0x000000010b7e02d1 -[NSInvocation invoke] + 289
    25  JavaScriptCore                      0x000000010e849933 _ZN3JSC24ObjCCallbackFunctionImpl4callEP9JSContextP13OpaqueJSValuemPKPKS3_PS6_ + 451
    26  JavaScriptCore                      0x000000010e848c52 _ZN3JSCL34objCCallbackFunctionCallAsFunctionEPK15OpaqueJSContextP13OpaqueJSValueS4_mPKPKS3_PS6_ + 290
    27  JavaScriptCore                      0x000000010e8491f6 _ZN3JSC19APICallbackFunction4callINS_20ObjCCallbackFunctionEEExPNS_9ExeTStateE + 598
    28  JavaScriptCore                      0x000000010e80abac _ZN3JSC5LLInt9setUpCallEPNS_9ExeTStateEPNS_11InstructionENS_22CodeSpecializationKindENS_7JSValueEPNS_17LLIntCallLinkInfoE + 604
    29  JavaScriptCore                      0x000000010e813397 llint_entry + 24953
    30  JavaScriptCore                      0x000000010e8133a5 llint_entry + 24967
    31  JavaScriptCore                      0x000000010e80d03b vmEntryToJavaScript + 299
    32  JavaScriptCore                      0x000000010e68e67e _ZN3JSC7JITCode7executeEPNS_2VMEPNS_14ProtoCallFrameE + 158
    33  JavaScriptCore                      0x000000010e65264c _ZN3JSC11Interpreter11executeCallEPNS_9ExeTStateEPNS_8JSObjectENS_8CallTypeERKNS_8CallDataENS_7JSValueERKNS_7ArgListE + 412
    34  JavaScriptCore                      0x000000010e38b3a9 _ZN3JSC12profiledCallEPNS_9ExeTStateENS_15ProfilingReasonENS_7JSValueENS_8CallTypeERKNS_8CallDataES3_RKNS_7ArgListE + 169
    35  JavaScriptCore                      0x000000010e752954 JSObjectCallAsFunction + 692
    36  JavaScriptCore                      0x000000010e7a92ff -[JSValue callWithArguments:] + 303
    37  Container                           0x000000010a5d40e4 __56-[TSCDependencyLoader loadDependencies:success:failure:]_block_invoke.175 + 644
    38  MAF                                 0x000000010a17026d -[TSGDataService deliverDataObject:error:forRequest:onSuccess:onError:] + 349
    39  MAF                                 0x000000010a16f362 -[TSGDataService handleDataObject:request:requestDate:response:error:onSuccess:onError:] + 738
    40  MAF                                 0x000000010a16b806 __52-[TSGDataService dispatchRequest:onSuccess:onError:]_block_invoke_2 + 198
    41  MAF                                 0x000000010a173e72 __75-[TSGQueuingDataService startDataLoader:forRequest:requestDate:completion:]_block_invoke_2 + 162
    42  MAF                                 0x000000010a16ecf1 __82-[TSGDataService parseResponse:withData:forRequest:inDataObject:error:completion:]_block_invoke_2 + 993
    43  libdispatch.dylib                   0x000000010fdb0978 _dispatch_call_block_and_release + 12
    44  libdispatch.dylib                   0x000000010fdda0cd _dispatch_client_callout + 8
    45  libdispatch.dylib                   0x000000010fdba8a4 _dispatch_main_queue_callback_4CF + 406
    46  CoreFoundation                      0x000000010b81de49 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    47  CoreFoundation                      0x000000010b7e337d __CFRunLoopRun + 2205
    48  CoreFoundation                      0x000000010b7e2884 CFRunLoopRunSpecific + 420
    49  GraphiTSServices                    0x0000000112adea6f GSEventRunModal + 161
    50  UIKit                               0x000000010bc7dc68 UIApplicationMain + 159
    51  Container Offline                   0x000000010a07dc9f main + 111
    52  libdyld.dylib                       0x000000010fe2668d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Expected Results

Objects should be returned. I have the feeling I am missing a step, so please let me know if I am missing something.

Actual Results

Error above.

Version of Realm and Tooling

Realm version: 2.5.0

Xcode version: 8.3.1

iOS/OSX version: 10.3

karagraysen commented 7 years ago

Hey @bontoJR. Thanks for reaching out about this. Someone will review what you've provided and follow-up soon with a solution or more questions.

bdash commented 7 years ago

It looks from the code that you've provided that you're attempting to use a number of internal Realm methods. These methods are implementation details of the Realm framework, are not intended to be used directly, and do not work in the manner that your code sample assumes they work (in particular, assigning to RLMRealm.schema after opening the Realm isn't useful).

If you can provide more detail about what you're trying to do we can try to point you at a supported way to accomplish that goal.

bontoJR commented 7 years ago

I am trying to create a dynamic way to create Realms and schemes at runtime, that's why I ended up experimenting with these methods.

The goal is to have a JSContext defining the scheme, objects and properties, which will then be used to manage the data. The JSContext will act as "front-end" and the Realm in Obj-C as "back-end" inside the app itself. This is what I am trying to achieve right now.

Side Note: I can't use React Native.

bdash commented 7 years ago

We don't have any public support for creating schemas at runtime. If you want to rely on Realm internals, which are subject to change at any time, you can likely use -[RLMRealmConfiguration setCustomSchema:] instead of -[RLMRealm setSchema:], and then pass the RLMRealmConfiguration instance with the custom schema configured on it to +[RLMRealm realmWithConfiguration:error:]. You can see examples of this in our unit tests.