领域:如何在迁移过程中修复“无效的属性名称” RLMException

时间:2019-05-31 16:05:09

标签: ios swift realm realm-mobile-platform

我的iOS应用使用RealmSwift,并且在从旧版本的应用迁移时崩溃。错误是:RLMException: Invalid Property Name <name> for class <class>

由于以下迁移顺序,崩溃似乎正在发生:

初始:创建了一个模型(我们称之为故事)

class Story: Object, Mappable {

    @objc dynamic var _id = ""
    @objc dynamic var lastModified: Date?
    @objc dynamic var name = ""
    @objc dynamic var content = ""

    override static func primaryKey() -> String? {
        return ServerKey.id
    }

    override static func indexedProperties() -> [String] {
        return ["name"]
    }
}

迁移X:使用migration.create(称为Book)创建了另一个模型,其中包含在上一步中创建的模型(Book包含Story)

private class func migrateToVersionX(_ migration: Migration) {
    migration.enumerateObjects( <perform unrelated migration to change some values on story objects> )
    migration.create("Book", value: ["stories": <array containing stories>)
}

迁移X + 2:向Story(Story.author)添加了一个新字段

class Story: Object, Mappable {

    @objc dynamic var _id = ""
    @objc dynamic var lastModified: Date?
    @objc dynamic var name = ""
    @objc dynamic var content = ""
    @objc dynamic var author = ""


    override static func primaryKey() -> String? {
        return ServerKey.id
    }

    override static func indexedProperties() -> [String] {
        return ["name", "author"]
    }
}

当用户使用的版本早于MigrationX时,应用程序崩溃并显示以下错误(此示例已修改)

'RLMException', reason: 'Invalid property name 'author' for class 'Story'.'

该应用在migration.create调用时崩溃。这是堆栈跟踪

Fatal Exception: RLMException
Invalid property name 'author' for class 'Story'.
0   CoreFoundation  
__exceptionPreprocess
1   libobjc.A.dylib 
objc_exception_throw
2   Realm   
RLMAccessor.mm line 567
RLMDynamicGetByName
3   RealmSwift  
Object.swift line 362
DynamicObject.subscript.getter
4   RealmSwift  
<compiler-generated> line 0
@objc DynamicObject.value(forUndefinedKey:)
5   Foundation  
-[NSObject(NSKeyValueCoding) valueForKey:]
6   Realm   
RLMObjectBase.mm line 174
-[RLMObjectBase valueForKey:]
7   Realm   
RLMObjectBase.mm line 451
RLMValidatedValueForProperty
8   Realm   
RLMAccessor.mm line 633
RLMAccessorContext::propertyValue(objc_object*, unsigned long,         RLMProperty*)
9   Realm   
RLMAccessor.mm line 776
RLMAccessorContext::value_for_property(objc_object*,     
std::__1::basic_string<char, std::__1::char_traits<char>, 
std::__1::allocator<char> > const&, unsigned long)
10  Realm   
object_accessor.hpp line 267
realm::Object realm::Object::create<objc_object* __strong,     RLMAccessorContext>(RLMAccessorContext&, std::__1::shared_ptr<realm::Realm>     const&, realm::ObjectSchema const&, objc_object* __strong, bool,     realm::BasicRow<realm::Table>*)
11  Realm   
row.hpp line 759
RLMCreateObjectInRealmWithValue
12  Realm   
RLMAccessor.mm line 752
realm::BasicRowExpr<realm::Table>     RLMAccessorContext::unbox<realm::BasicRowExpr<realm::Table> >(objc_object*, bool, bool)
13  Realm   
list.hpp line 192
auto realm::List::dispatch<void realm::List::add<objc_object* __strong&, RLMAccessorContext>(RLMAccessorContext&, objc_object* __strong&&&, bool)::'lambda'(objc_object* __strong&)>(objc_object* __strong&&&) const
14  Realm   
RLMAccessor.hpp line 77
void RLMAccessorContext::enumerate_list<void     realm::List::assign<objc_object* __strong&, RLMAccessorContext>(RLMAccessorContext&, objc_object* __strong&&&, bool)::'lambda'(objc_object* __strong&&&)>(objc_object*, objc_object* __strong&&&)
15  Realm   
list.hpp line 220
void realm::List::assign<objc_object* __strong&, RLMAccessorContext> (RLMAccessorContext&, objc_object* __strong&&&, bool)
16  Realm   
object_accessor.hpp line 94
void realm::Object::set_property_value_impl<objc_object* __strong,     RLMAccessorContext>(RLMAccessorContext&, realm::Property const&,     objc_object* __strong, bool, bool)
17  Realm   
object_accessor.hpp line 281
realm::Object realm::Object::create<objc_object* __strong,     RLMAccessorContext>(RLMAccessorContext&, std::__1::shared_ptr<realm::Realm> const&, realm::ObjectSchema const&, objc_object* __strong, bool, realm::BasicRow<realm::Table>*)
18  Realm   
row.hpp line 759
RLMCreateObjectInRealmWithValue
19  Realm   
RLMRealm.mm line 840
-[RLMRealm createObject:withValue:]
20  Realm   
RLMMigration.mm line 135
-[RLMMigration createObject:withValue:]
21  RealmSwift  
Migration.swift line 129
Migration.create(_:value:)
22  Project Name    
MigrationHelper.swift line 187
specialized static MigrationHelper.migrateToVersionX(_:)

我认为发生的是,即使在MigrationX + 2之前才正式引入它,MigrationX中的migration.create函数正在查看Story类并查找作者字段。

任何对为什么发生此崩溃以及如何适当处理这种情况的想法都将不胜感激。

0 个答案:

没有答案