2017-03-26 53 views
0

我得到 bool read_only() const { return schema_mode == SchemaMode::ReadOnly; }境界BAD_ACCESS錯誤

錯誤BAD_ACCESS當這種方法被稱爲:

- (void) writeItems: (NSArray *) rmItems { 
    dispatch_async(self.backgroundQueue, ^{ 
     RLMRealm *realm = [[RLMRealm alloc] init]; 
     [realm beginWriteTransaction]; 
     [realm addObjects:rmItems]; 
     [realm commitWriteTransaction]; 
    }); 
} 

使用領域版本:2.4.3 Objective-C的項目

境界是通過安裝CocoaPods

我使用單例類來處理領域:

@implementation RMDataManager 

+ (id)sharedManager { 
    static RMDataManager *sharedManager_ = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     sharedManager_ = [[self alloc] init]; 
    }); 
    return sharedManager_; 
} 

- (instancetype)init { 
    self = [super init]; 

    if(self) { 
     [self setVersionAndMigrations]; 
    } 
    return self; 
} 

- (void)setVersionAndMigrations { 
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration]; 

    // Set the new schema version. This must be greater than the previously used 
    // version (if you've never set a schema version before, the version is 0). 
    config.schemaVersion = 1; 

    // Set the block which will be called automatically when opening a Realm with a 
    // schema version lower than the one set above 
    config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) { 
     // We haven’t migrated anything yet, so oldSchemaVersion == 0 
     if (oldSchemaVersion < 1) { 
      // Nothing to do! 
      // Realm will automatically detect new properties and removed properties 
      // And will update the schema on disk automatically 
     } 
    }; 

    // Tell Realm to use this new configuration object for the default Realm 
    [RLMRealmConfiguration setDefaultConfiguration:config]; 

    // Now that we've told Realm how to handle the schema change, opening the file 
    // will automatically perform the migration 
    [RLMRealm defaultRealm]; 
} 

- (dispatch_queue_t) backgroundQueue { 
    if(!_backgroundQueue) _backgroundQueue = dispatch_queue_create("RealmBackground", nil); 
    return _backgroundQueue; 
} 

- (void)clearAllData { 
    dispatch_async(self.backgroundQueue, ^{ 
     RLMRealm *realm = [[RLMRealm alloc] init]; 
     // Delete all objects from the realm 
     [realm beginWriteTransaction]; 
     [realm deleteAllObjects]; 
     [realm commitWriteTransaction]; 

    }); 
} 

@end 

我該怎麼做?我已經閱讀領域文檔,找不到任何幫助我的信息。

回答

1

你自己並沒有實例化RLMRealm實例。

您通常使用[RLMRealm defaultRealm][RLMRealm realmWithConfiguration]創建它們,但不支持直接使用[[RLMRealm alloc] init]創建它們。

假如你只是使用默認域工作,你只需要你的代碼改成這樣:

- (void) writeItems: (NSArray *) rmItems { 
    dispatch_async(self.backgroundQueue, ^{ 
     RLMRealm *realm = [RLMRealm defaultRealm]; 
     [realm beginWriteTransaction]; 
     [realm addObjects:rmItems]; 
     [realm commitWriteTransaction]; 
    }); 
} 

我絕對推薦你讀Realms section of the documentation瞭解更多關於RLMRealm類。

+1

Thanx!我意識到這一點 – Ilya