在以下列方式发出NSInternalInconsistencyException
请求时,我的应用与PUT
崩溃:
HTTP GET
HTTP PUT
PUT
响应正文的正文2015-05-27 17:27:17.796 XYZ [17093:2526798] ***由于未捕获的异常而终止应用程序' NSInternalInconsistencyException',原因:'无法使用实体添加对象&# 39;公司'缓存'公司'
的实体
错误来自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
操作现在可以正常运行。我确实将我的映射实现为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
由于该问题似乎与多个NSManagedObjectContext
有关,因此以下是我设置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()
}
答案 0 :(得分:1)
现在奇怪的是,实体和self.entity对象都具有相同的调试描述。
实体描述的地址必须不同。
当您篡改RestKit正在使用的托管对象上下文时,通常会发生这种情况,因此被比较的实体来自2个不同的上下文,因此不同,或者直接从错误的上下文传递托管对象。
答案 1 :(得分:0)
您必须创建ManagedObjectStore。我怀疑self
作为参数是正确的。我从你的解释中推断出self是NSManagedObject的子类,即你要映射的实体,Company
或Profile
但是你需要RKManagedObjectStore
并将其作为第二个参数传递。< / p>
let mapping = RKEntityMapping(
forEntityForName: "Profile",
inManagedObjectStore: <pointer to the RKManagedObjectStore>
)