【发布时间】:2015-05-27 15:52:53
【问题描述】:
当以下列方式发出PUT 请求时,我的应用程序因NSInternalInconsistencyException 而崩溃:
- 通过
HTTP GET下载配置文件信息 - 更新客户端上的配置文件
- 通过
HTTP PUT上传更改 - 再次解析
PUT响应正文的正文
我得到的错误
2015-05-27 17:27:17.796 XYZ[17093:2526798] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将具有实体“公司”的对象添加到“公司”实体的缓存中''
错误来自这里RKEntityByAttributeCache.m#L333:
NSAssert([entity isKindOfEntity:self.entity], @"Cannot add object with entity '%@' to cache for entity of '%@'", [entity name], [self.entity name]);
现在奇怪的是,entity 和 self.entity 对象都具有相同的调试描述。所以我不知道这两个实例的NSEnityDescriptor 可能如何变化。
到目前为止我尝试过的事情
- 从模拟器中删除应用程序:然后我可以更新它一次,然后我再次收到上述错误。
- 删除
NSAssert语句:PUT操作现在可以正常工作了。 -
按照here 的描述禁用缓存(在 Restkit 0.24.1 中,这似乎不再可能)
我的映射
我确实将我的映射实现为RKManagedObjectStore 的扩展,如下所示:
extension RKManagedObjectStore{
var companyMapping: RKEntityMapping{
let mapping = RKEntityMapping(
forEntityForName: "Company",
inManagedObjectStore: self
)
mapping.addAttributeMappingsFromArray(
["id", "name"]
)
mapping.identificationAttributes = ["id"]
return mapping
}
var profileMapping: RKEntityMapping{
let mapping = RKEntityMapping(
forEntityForName: "Profile",
inManagedObjectStore: self
)
mapping.addAttributeMappingsFromArray(
["id", "content", "language", "profileImageId" ]
)
mapping.identificationAttributes = ["id"]
mapping.addRelationshipMappingWithSourceKeyPath("companies", mapping: companyMapping)
return mapping
}
}
我发布到服务器并以相同格式返回响应的数据:
{
"content": "XYZ",
"id": "profile-1",
"language": "de",
"companies": [{
"id": "company-1",
"name": "Somecompany Inc."
}]
}
更新NSMangedObectContext
由于该问题似乎与多个NSManagedObjectContexts 有关,因此我设置了 RestKit 和 MOC:
init(baseURL: String, dataModelName: String){
let modelURL = NSBundle.mainBundle().URLForResource(dataModelName, withExtension: "momd")!
let managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL)!
self.managedObjectStore = RKManagedObjectStore(managedObjectModel: managedObjectModel)
let storePath = RKApplicationDataDirectory().stringByAppendingPathComponent("\(dataModelName).sqlite")
var error: NSError? = nil
managedObjectStore.addSQLitePersistentStoreAtPath(storePath,
fromSeedDatabaseAtPath: nil,
withConfiguration: nil,
options: nil,
error: &error
)
if let e = error{
// Omitted: Delete the SQLite store and re-create it
}
managedObjectStore.createManagedObjectContexts()
managedObjectStore.managedObjectCache = RKInMemoryManagedObjectCache(
managedObjectContext: managedObjectStore.persistentStoreManagedObjectContext
)
objectManager = RKObjectManager(baseURL: NSURL(string: baseURL))
objectManager.managedObjectStore = managedObjectStore
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
super.init(baseURL: baseURL)
setupMapping()
}
【问题讨论】:
标签: ios swift core-data restkit