我在抓取时遇到了麻烦。使用sql编辑器检查我可以看到具有两个字段(整数位置和字符串weburl)的表格网在提取之前是空的。但是,在取得之后:
NSArray * arrwebs = [managedObjectContext
executeFetchRequest:allwebs error:&error];
它有一个元素:
"Error: (null)
2013-02-05 15:01:39.001 Assignment3-1[36607:c07] arrWwebs has 1 elements
2013-02-05 15:01:39.001 Assignment3-1[36607:c07] Postition: 0"
arrwebs不应该是空的吗?我之前删除了元素(通过编程和通过sql编辑器);
代码如下:
-(void) logElements
{
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = delegate.managedObjectContext;
NSManagedObject *newWebs;
newWebs = [NSEntityDescription insertNewObjectForEntityForName:@"Webs" inManagedObjectContext:managedObjectContext];
NSFetchRequest * allwebs = [[NSFetchRequest alloc] init];
[allwebs setEntity:[NSEntityDescription entityForName:@"Webs" inManagedObjectContext:managedObjectContext]];
[allwebs setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSError *error = nil;
NSArray * arrwebs = [managedObjectContext executeFetchRequest:allwebs error:&error];
NSLog(@"Error: %@", [error localizedDescription]);
NSLog(@"arrWwebs has %d elements", [arrwebs count]);
for (NSManagedObject * info in arrwebs) {
//[managedObjectContext deleteObject:webobject];
NSLog(@"Postition: %@", [info valueForKey:@"position"]);
NSLog(@"URL: %@", [info valueForKey:@"weburl"]);
}
if (![managedObjectContext save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
}
有什么想法吗?
答案 0 :(得分:1)
您正在插入Webs
对象newWebs
。所以结果是预期的。