领域BAD_ACCESS错误

时间:2017-03-26 16:21:05

标签: objective-c realm

我收到错误BAD_ACCESS bool read_only() const { return schema_mode == SchemaMode::ReadOnly; }

调用此方法时:

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

使用Realm版本:2.4.3与Objective-C项目

realm是通过CocoaPods安装的

我使用singleton类来处理领域:

@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 个答案:

答案 0 :(得分:1)

您自己并未实例化RLMRealm个实例。

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

假设您只是使用默认的Realm,您只需将代码更改为:

- (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课程的更多信息。