1

我在获取时遇到了麻烦。使用 sql 编辑器检查,我可以看到具有两个字段(整数位置和字符串 weburl)的表 web 在获取之前为空。但是,在获取之后:

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]);
    }    
}

任何想法?

4

1 回答 1

1

您将Webs对象插入为newWebs. 所以结果是意料之中的。

于 2013-02-05T15:12:42.043 回答