2013-02-05 141 views
1

我在抓取時遇到問題。使用sql編輯器進行檢查,我可以看到具有兩個字段(整數位置和字符串weburl)的表格網格在獲取之前是空的。然而,取後:CoreData:刪除所有元素後managedObjectContext上的奇怪元素

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

任何想法?

回答

1

您插入的Webs對象爲newWebs。所以預期結果。

+0

歡迎來到Stackoverflow,archipestre。自定義勾選複選標記以獲得正確或有用的答案。請儘快執行此操作。 – Mundi

+0

謝謝蒙迪。就是這樣。 – archipestre