从另一个方法从NSManagedObjectContext中删除对象 [英] Delete objects from NSManagedObjectContext from another method

查看:199
本文介绍了从另一个方法从NSManagedObjectContext中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除所有的对象,但我似乎无法使它的工作。我知道这种问题还有其他问题,但他们没有帮助。我想要的是在这个方法中删除section和fixture实体中的所有对象。

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

这是我在另一个方法中保存和获取对象的地方。

  NSManagedObjectContext * context = [self managedObjectContext]; 

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

fixture = [NSEntityDescription insertNewObjectForEntityForName:@FixtureinManagedObjectContext: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:@SectioninManagedObjectContext:context ];
[lolSection setValue:[NSString stringWithFormat:@%@,[[section 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:@selfascending:NO];
NSArray * descriptors = [NSArray arrayWithObject:descriptor];
NSArray * reverseTheArray = [[self.theSection valueForKey:@date] sortedArrayUsingDescriptors:descriptors];
reversedArray = [[reverseTheArray reverseObjectEnumerator] allObjects];


解决方案

p>

根据文档。


删除托管对象很简单。您只需发送
管理对象上下文a deleteObject:消息,传递您
要删除的对象作为参数。


换句话说,使用有效的上下文表示

  [aValidContext deleteObject:managedObjectYouWantToDelete]; 

关于您的问题,您需要


  1. 设置抓取请求以抓取 Fixture 项目

  2. 循环结果c> NSArray )并执行删除

例如

  NSFetchRequest * request = //在这里创建请求... 

NSError * error;
NSArray * array = [aValidContext executeFetchRequest:request error:& error];
if(array == nil){
//处理错误...
} else {
//循环数组执行deleteObject
}

//保存到持久存储

剩下的代码留待练习;)

编辑1



再次阅读您的问题, Section Fixture ?如果它们是分开的实体,您还应为执行以前的步骤。


I'm trying to delete all the objects, but I can't seem to make it work. I know there is other questions for this kind of question, but they are not helpful. What I want is to delete all the objects in section and fixture entity in this method.

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

This is where I save and fetch my objects in another method.

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

解决方案

So, what have you tried so far?

As per the documentation.

Deleting a managed object is straightforward. You simply send its managed object context a deleteObject: message, passing the object you want to delete as the argument.

In other words, using a valid context you shoul say

[aValidContext deleteObject:managedObjectYouWantToDelete];

About your question, you need to

  1. Setup a fetch request to grab the Fixture items
  2. Loop the results (an NSArray) and perform the deletion

For example

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

Remaining code is left for exercise ;)

Edit 1

Reading again your question, what is the relationship between Section and Fixture? If they are separated entities, you should perform previous steps also for Sections.

这篇关于从另一个方法从NSManagedObjectContext中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆