我正在解析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];
}发布于 2015-05-28 04:08:55
这里没有显示删除的代码。
你说不能删除,但你没说为什么或怎么删除。失败的原因是什么?您的期望是什么?实际结果是什么?
mergeChanges:是什么样子的?
请更新您的问题,以便我们有机会了解问题。
https://stackoverflow.com/questions/30484008
复制相似问题