2017-10-05 2 views
0

Ich versuche Realm-Modell mit Ziel C zu lernen. Ich möchte wissen, wie man unsere eigene .realm-Datei erstellt und die Realm-Datei im Realm-Browser sieht. Folgendes ist mein Code.Wie erstelle ich mein eigenes Realm-Modell?

-In Specimen.h

#import <Foundation/Foundation.h> 
#import <Realm/Realm.h> 

@interface Specimen : RLMObject//: NSObject 
    @property NSString *name; 
    @property NSString *specDescription; 
    @property NSInteger latitude; 
    @property NSInteger longitude; 
    @property NSDate *date; 
@end 

-In UIViewController.m

#import "ViewController.h" 
#import "Specimen.h" 


@interface ViewController() 
{ 
    Specimen *first; 
} 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    first = [[Specimen alloc] init]; 

    first.name = @"first specimen"; 
    first.specDescription = @"some description"; 
    first.latitude = 12; 
    first.longitude = 15; 
    first.date = [NSDate date]; 

    RLMRealm *realm = [RLMRealm defaultRealm]; 
    [realm transactionWithBlock:^{ 
     [realm addObject:first]; 
    }]; 

    NSLog(@"Object added in realm"); 
} 

gebaut wird gelungen. Auch das letzte Protokoll wird auf der Konsole angezeigt. Aber ich verstehe nicht, wo das Objekt Objekt zu sehen ist, da der Standardbereich immer nur Objekte für Personen und Hunde enthält. Also muss ich wissen, wie ich meinen eigenen Realm erstellen und dann das Objekt hinzufügen und über den Realm-Browser darauf zugreifen kann.

Antwort

0

Von Reich official document.

Konfigurieren Dinge wie, wo Ihre Realm-Dateien gespeichert werden, wird durch RLMRealmConfiguration getan.

Beispiel:

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration]; 

// Use the default directory, but replace the filename with the username 
config.fileURL = [[[config.fileURL URLByDeletingLastPathComponent] 
         URLByAppendingPathComponent:username] 
         URLByAppendingPathExtension:@"realm"]; 

// Set this as the configuration used for the default Realm 
[RLMRealmConfiguration setDefaultConfiguration:config]; 

Sie mehrere Konfigurationsobjekte haben können, so dass Sie die Version steuern kann, Schema und Lage jedes Realm unabhängig:

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration]; 

// Get the URL to the bundled file 
config.fileURL = [[NSBundle mainBundle] URLForResource:@"MyBundledData" withExtension:@"realm"]; 

// Open the file in read-only mode as application bundles are not writeable 
config.readOnly = YES; 

// Open the Realm with the configuration 
RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil]; 

// Read some data from the bundled Realm 
RLMResults<Dog *> *dogs = [Dog objectsInRealm:realm where:@"age > 5"]; 
+0

Danke :) Wird es versuchen. – pz26

Verwandte Themen