2016-06-17 27 views
1

Ich füge Realm Quelldateien in meinem Projekt hinzu.
Ich möchte Realm-Klasse dynamisch erstellen.
Folgende ist mein Demo-Code:Wie dynamisch erstellte Klasse in Realm retrive?

-(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"]); 

} 

während schema2 Erstellen App abstürzt.

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 

Wie kann man überprüfen, ob TrulyDynamicObject erstellt wird oder nicht, als wenn ich nur [self createDynamicSchema]; mit ausgeführtem Code funktioniert es gut.

Jeder weiß, wie man mehrere Schemas mit Realm-Objekt als Referenz erstellt, wenn die Klasse dynamisch erstellt wird?

Bitte helfen!

Antwort

0

Von der „Optional Eigenschaften“ von Realm docs (link):

RLMObject Unterklasse Eigenschaften können immer gleich Null sein und kann somit nicht in requiredProperties enthalten sein und RLMArray nicht unterstützt nil speichern.

übersetzt in RLMProperty Formulierung, bedeutet dies, dass die Eigenschaften des Typs RLMPropertyTypeObject optional sein muss, da das zugrunde liegende Datenformat nicht, dass es dort einen Link wird immer garantieren kann.

Verwandte Themen