【问题标题】:How to retrive dynamically created Class in Realm?如何在领域中检索动态创建的类?
【发布时间】:2016-06-17 09:47:50
【问题描述】:

我在我的项目中添加 Realm 源文件。我想动态创建领域类。以下是我的演示代码:

-(void)createDynamicClassObject
{
    [self createDynamicSchema];
    [self createDynamicSchema2];
}

-(void)createDynamicSchema{

    _schema = [[RLMSchema alloc] init];
    RLMProperty *prop =  [[RLMProperty alloc] initWithName:@"a"
                                                     type:RLMPropertyTypeInt
                                          objectClassName:nil
                                   linkOriginPropertyName:nil
                                                  indexed:NO
                                                 optional:NO];
    RLMObjectSchema *objectSchema1 = [[RLMObjectSchema alloc] initWithClassName:@"TrulyDynamicObject"
                                                                   objectClass:RLMObject.class properties:@[prop]];


    _schema.objectSchema = @[objectSchema1];
    NSLog(@"dyrealm %@",_dyrealm);

    NSLog(@"schema %@",[_schema schemaForClassName:@"TrulyDynamicObject"]);
}

-(void)createDynamicSchema2 {

    RLMProperty *prop1 =  [[RLMProperty alloc] initWithName:@"apple"
                                                      type:RLMPropertyTypeString
                                           objectClassName:nil
                                    linkOriginPropertyName:nil
                                                   indexed:NO
                                                  optional:NO];
    RLMProperty *prop2 =  [[RLMProperty alloc] initWithName:@"banana"
                                                       type:RLMPropertyTypeFloat
                                            objectClassName:nil
                                     linkOriginPropertyName:nil
                                                    indexed:NO
                                                   optional:NO];

    RLMProperty *prop3 =  [[RLMProperty alloc] initWithName:@"Mango"
                                                       type:RLMPropertyTypeObject
                                            objectClassName:@"TrulyDynamicObject"
                                     linkOriginPropertyName:nil
                                                    indexed:NO
                                                   optional:NO];

    RLMObjectSchema *objectSchema = [[RLMObjectSchema alloc] initWithClassName:@"DynamicTestObject"
                                                                   objectClass:RLMObject.class properties:@[prop1,prop2,prop3]];

    NSMutableArray *array = [NSMutableArray arrayWithArray:_schema.objectSchema];
    [array addObject:objectSchema];
    _schema.objectSchema = array;

    _dyrealm = [self realmWithTestPathAndSchema:_schema];

    NSLog(@"DynamicTestObject dyrealm %@",_dyrealm);
    NSLog(@"DynamicTestObject schema %@",[_schema schemaForClassName:@"DynamicTestObject"]);

}

在创建 schema2 时,应用程序崩溃了。

2016-06-17 15:10:13.491 realmLibraryDemo[1770:82980] schema TrulyDynamicObject {
    a {
        type = int;
        objectClassName = (null);
        linkOriginPropertyName = (null);
        indexed = NO;
        isPrimary = NO;
        optional = NO;
    }
}
2016-06-17 15:10:13.499 realmLibraryDemo[1770:82980] *** Terminating app due to uncaught exception 'RLMException', reason: 'Schema validation failed due to the following errors:
- 'Object' property 'Mango' must be nullable.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010217ae65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000101bf1deb objc_exception_throw + 48
    2   Realm                               0x0000000101281c49 _Z18RLMSetErrorOrThrowP7NSErrorPU15__autoreleasingS0_ + 985
    3   Realm                               0x00000001012606e6 _Z26RLMRealmTranslateExceptionPU15__autoreleasingP7NSError + 598
    4   Realm                               0x0000000101260dfc +[RLMRealm openSharedRealm:error:] + 268
    5   Realm                               0x0000000101261e66 +[RLMRealm realmWithConfiguration:error:] + 4022
    6   realmLibraryDemo                    0x0000000100fa4eb9 -[ViewController realmWithTestPathAndSchema:] + 217
    7   realmLibraryDemo                    0x0000000100fa4cb8 -[ViewController createDynamicSchema2] + 776
    8   realmLibraryDemo                    0x0000000100fa46f6 -[ViewController createDynamicClassObject] + 70
    9   realmLibraryDemo                    0x0000000100fa42d0 -[ViewController viewDidLoad] + 288
    10  UIKit                               0x00000001026bef98 -[UIViewController loadViewIfRequired] + 1198
    11  UIKit                               0x00000001026bf2e7 -[UIViewController view] + 27
    12  UIKit                               0x0000000102595ab0 -[UIWindow addRootViewControllerViewIfPossible] + 61
    13  UIKit                               0x0000000102596199 -[UIWindow _setHidden:forced:] + 282
    14  UIKit                               0x00000001025a7c2e -[UIWindow makeKeyAndVisible] + 42
    15  UIKit                               0x0000000102520663 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4131
    16  UIKit                               0x0000000102526cc6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1760
    17  UIKit                               0x0000000102523e7b -[UIApplication workspaceDidEndTransaction:] + 188
    18  FrontBoardServices                  0x0000000104f28754 -[FBSSerialQueue _performNext] + 192
    19  FrontBoardServices                  0x0000000104f28ac2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    20  CoreFoundation                      0x00000001020a6a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    21  CoreFoundation                      0x000000010209c95c __CFRunLoopDoSources0 + 556
    22  CoreFoundation                      0x000000010209be13 __CFRunLoopRun + 867
    23  CoreFoundation                      0x000000010209b828 CFRunLoopRunSpecific + 488
    24  UIKit                               0x00000001025237cd -[UIApplication _run] + 402
    25  UIKit                               0x0000000102528610 UIApplicationMain + 171
    26  realmLibraryDemo                    0x0000000100fa562f main + 111
    27  libdyld.dylib                       0x00000001048ce92d start + 1
    28  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

当我只使用[self createDynamicSchema]; 执行代码时,如何检查是否创建了TrulyDynamicObject,它工作正常。

在动态创建类时,任何人都知道如何使用领域对象作为参考创建多个模式?

【问题讨论】:

    标签: ios objective-c xcode class realm


    【解决方案1】:

    来自 Realm 文档 (link) 的“可选属性”部分:

    RLMObject 子类属性总是可以为零,因此不能包含在requiredProperties 中,并且RLMArray 不支持存储nil

    翻译成RLMProperty 措辞,意味着RLMPropertyTypeObject 类型的属性必须是可选的,因为底层数据格式不能保证那里总是有链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多