2014-03-25 70 views
0

我想刪除所有的對象,但我似乎無法使其工作。我知道這類問題還有其他問題,但它們沒有幫助。我想要的是刪除該方法中的部分和夾具實體中的所有對象。從另一種方法從NSManagedObjectContext刪除對象

- (void)refresh:(UIRefreshControl *)refreshControl {  
    [refreshControl endRefreshing]; 
} 

這是我用另一種方法保存和獲取我的對象的地方。

NSManagedObjectContext *context = [self managedObjectContext]; 

for (int i=0; i <=fixtures.count-1; i++) { 

    fixture = [NSEntityDescription insertNewObjectForEntityForName:@"Fixture" inManagedObjectContext:context]; 

    [fixture setValue:[NSString stringWithFormat:@"%@",[[fixtures objectAtIndex:i] objectForKey:@"date"]] forKey:@"date"]; 
    [fixture setValue:[[fixtures objectAtIndex:i] objectForKey:@"time"] forKey:@"time"]; 
    [fixture setValue:[[fixtures objectAtIndex:i] objectForKey:@"home"] forKey:@"home"]; 
    [fixture setValue:[[fixtures objectAtIndex:i] objectForKey:@"away"] forKey:@"away"]; 
    [fixture setValue:[[fixtures objectAtIndex:i] objectForKey:@"league"] forKey:@"league"]; 
} 

for (int i=0; i <=sections.count-1; i++) { 
    lolSection = [NSEntityDescription insertNewObjectForEntityForName:@"Section" inManagedObjectContext:context]; 
    [lolSection setValue:[NSString stringWithFormat:@"%@",[[sections objectAtIndex:i] objectForKey:@"date"]] forKey:@"date"]; 
} 

NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Fixture"]; 
self.theFixtures = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 

NSFetchRequest *fetchRequest2 = [[NSFetchRequest alloc] initWithEntityName:@"Section"]; 
self.theSection= [[managedObjectContext executeFetchRequest:fetchRequest2 error:nil] mutableCopy]; 

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO]; 
NSArray *descriptors = [NSArray arrayWithObject: descriptor]; 
NSArray* reverseTheArray = [[self.theSection valueForKey:@"date"] sortedArrayUsingDescriptors:descriptors]; 
reversedArray = [[reverseTheArray reverseObjectEnumerator] allObjects]; 

回答

0

那麼,你到目前爲止嘗試過什麼?

根據文檔。

刪除託管對象很簡單。您只需發送其 管理的對象上下文一個deleteObject:消息,將您想要刪除的對象作爲參數傳遞給您。

換句話說,使用有效的環境下,你建議立即進行刪除說

[aValidContext deleteObject:managedObjectYouWantToDelete]; 

關於你的問題,你需要

  1. 設置爲獲取請求搶Fixture項目
  2. 循環結果(一個NSArray)並執行刪除操作

例如

NSFetchRequest* request = // create the request here... 

NSError *error; 
NSArray *array = [aValidContext executeFetchRequest:request error:&error]; 
if (array == nil) { 
    // Deal with error... 
} else { 
    // loop the array performing deleteObject 
} 

// save to persistent store 

其餘代碼留給鍛鍊;)

編輯1

重讀你的問題,什麼是SectionFixture之間的關係?如果他們是分離的實體,則應該執行Section s的先前步驟。