我正在解析xml,在数据库中插入一个实体,用于更新记录我再次添加该记录并删除旧记录。但它无法删除旧条目,因此我能够看到重复的条目。
- (void)insertSampleWithId:(NSString *)sampleId{
__block NSError *lError = nil;
dispatch_queue_t categoryCustomQueue = dispatch_queue_create("com.xyz.sample”, NULL);
dispatch_sync(categoryCustomQueue, ^{
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserver:self
selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:_tempMOC];
_supplier = [NSEntityDescription insertNewObjectForEntityForName:@"BSSample” inManagedObjectContext:_tempMOC];
_supplier.sampleId = supplierId;
// double delayInSeconds = 0.3;
// dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//
// dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//
// });
[self ifSupplierPresentInMainContextRemoveIt:supplierId];
if(![_tempMOC save:&lError]){
NSLog(@"Error adding while category %@ ,%@ ,%@", lError, [lError userInfo], [lError localizedDescription]);
}
[dnc removeObserver:self name:NSManagedObjectContextDidSaveNotification object:_tempMOC];
});
}
- (BOOL)ifSamplePresentInMainContextRemoveIt:(NSString *)sampleId{
__block BSSampler *suppl = nil;
NSManagedObjectContext *managedObjectContext = dataManager.coreDataHelper.context;
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"BSSample” inManagedObjectContext:managedObjectContext];
[_fetchRequest setEntity:entityDescription];
[_fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"sampleId == '%@'", supplierId]];
[_fetchRequest setResultType:NSDictionaryResultType];
[_fetchRequest setReturnsDistinctResults:YES];
NSError *error;
NSArray *array = [managedObjectContext executeFetchRequest:_fetchRequest error:&error];
if(array != nil && [array count]>0){
suppl = [array objectAtIndex:0];
}
if(suppl)
[dataManager.coreDataHelper.context deleteObject:suppl];
return [dataManager.coreDataHelper.context save:&error];
}
- (void)mergeChanges:(NSNotification *)saveNotification{
[self performSelectorOnMainThread:@selector(mergeToMainThred:) withObject:saveNotification waitUntilDone:NO];
}
- (void)mergeToMainThred:(NSNotification*)saveNotification{
[dataManager.coreDataHelper.context mergeChangesFromContextDidSaveNotification:saveNotification];
}
答案 0 :(得分:0)
此处没有显示您删除的代码。
你说无法删除,但你不能说出原因或方法。什么失败了?您的期望是什么?实际结果是什么?
mergeChanges:
看起来像什么?
请更新您的问题,以便我们有机会了解此问题。