2015-07-01 15 views
2

发生了什么的说明:用户在另一个视图中添加了作业作为他们的最爱。现在用户位于“收藏夹”选项卡中,并决定他们不再希望将作业作为他们的最爱之一,因此他们轻扫以删除作业。他们点击删除按钮,发生下面的错误...代码按原样运行,但它也会删除用户保存为收藏夹的每个作业,而不是仅删除一项作业。PFRelation从PFRelation中删除一个对象,并非全部

我的代码也给我一个警告:

警告:长时间运行的操作被主线程上执行。 中断warnBlockingOperationOnMainThread()以进行调试。

#import "JobDetailViewController.h" 
#import "MyFavoritesTableViewController.h" 
#import "Parse/Parse.h" 
#import "Job.h" 
#import "JobListViewController.h" 

@interface MyFavoritesTableViewController() 

@property (nonatomic, strong) NSString *mainTitle; 
@property (nonatomic, strong) NSString *subTitle; 

@end 

@interface MyFavoritesTableViewController() 

@end 

@implementation MyFavoritesTableViewController 
{} 

@synthesize mainTitle; 
@synthesize subTitle; 



- (id)initWithCoder:(NSCoder *)aCoder 
{ 
    self = [super initWithCoder:aCoder]; 
    if ([PFUser currentUser]) { 
     // Custom the table 

     // The className to query on 
     self.parseClassName = @"Jobs"; 

     // The key of the PFObject to display in the label of the default cell style 
     self.textKey = @"Position"; 

     // Whether the built-in pull-to-refresh is enabled 
     self.pullToRefreshEnabled = YES; 

     // Whether the built-in pagination is enabled 
     self.paginationEnabled = YES; 

     // The number of objects to show per page 
     self.objectsPerPage = 30; 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 


} 
- (void)objectsWillLoad { 
    [super objectsWillLoad]; 
} 

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    [self.tableView reloadData]; 

} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object: (PFObject *)object 
{ 
    static NSString *myJobsTableIdentifier = @"myFavsCell"; 


    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:myJobsTableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myJobsTableIdentifier]; 
    } 

    // Configure the cell 
    PFFile *thumbnail = [object objectForKey:@"imageFile"]; 
    PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100]; 
    thumbnailImageView.image = [UIImage imageNamed:@"AppIcon.png"]; 
    thumbnailImageView.file = thumbnail; 
    [thumbnailImageView loadInBackground]; 

    UILabel *positionLabel = (UILabel*) [cell viewWithTag:101]; 
    positionLabel.text = [object objectForKey:@"Position"]; 
    UILabel *rotationLabel = (UILabel*) [cell viewWithTag:102]; 
    rotationLabel.text = [object objectForKey:@"Rotation"]; 
    UILabel *locationLabel = (UILabel*) [cell viewWithTag:103]; 
    locationLabel.text = [object objectForKey:@"Location"]; 
    UILabel *typeLabel = (UILabel*) [cell viewWithTag:104]; 
    typeLabel.text = [object objectForKey:@"Type"]; 
    return cell; 

} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"showDetailedView"]) { 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 

     Job *job = [[Job alloc] init]; 

     JobDetailViewController *destViewController = segue.destinationViewController; 

     PFObject *object = [self.objects objectAtIndex:indexPath.row]; 
     job.position = [object objectForKey:@"Position"]; 
     job.poc = [object objectForKey:@"POC"]; 
     job.email = [object objectForKey:@"Email"]; 
     job.phone = [object objectForKey:@"Phone"]; 
     job.apply = [object objectForKey:@"Apply"]; 
     job.imageFile = [object objectForKey:@"imageFile"]; 
     job.rotation = [object objectForKey:@"Rotation"]; 
     job.location = [object objectForKey:@"Location"]; 
     job.type = [object objectForKey:@"Type"]; 
     job.clearance = [object objectForKey:@"Clearance"]; 
     job.job_description = [object objectForKey:@"Job_Description"]; 
     job.qualifications = [object objectForKey:@"Qualifications"]; 
     job.originalJob = object; 
     destViewController.job = job; 
    } 
} 

#pragma mark - UITableViewDelegate 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    if ([self.objects count] == indexPath.row) { 
     [self loadNextPage]; 
    } else { 
     PFObject *photo = [self.objects objectAtIndex:indexPath.row]; 
     NSLog(@"%@", photo); 

     // Do something you want after selected the cell 
    } 
} 

- (PFQuery *)queryForTable 

{ 
    PFUser *user = [PFUser currentUser]; 
    PFRelation *relation = [user relationForKey:@"Favorites"]; 
    PFQuery *myquery = [relation query]; 
    if (self.pullToRefreshEnabled) { 
     myquery.cachePolicy = kPFCachePolicyNetworkOnly; 
    } 
    return myquery; 

} 

#pragma mark - DeleteJobViewDelegate 


    - (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath { 
    PFUser *user = [PFUser currentUser]; 
    PFRelation *relation = [user relationForKey:@"Favorites"]; 
    PFQuery *myquery = [relation query]; 
    NSArray *array = [myquery findObjects]; 
     for (PFObject *object in array) 
     { 
       [relation removeObject:object]; 
     } 

    [user saveInBackground]; 

    [self.tableView reloadData]; 
     [self loadObjects]; 
    } 

    @end 
+0

由于某些原因,如果从收藏夹中删除一个作业,它会从收藏夹中删除所有作业。 –

+0

在你的循环中你将全部删除。使用saveEventually,这将消除错误的问题。 – DogCoffee

+0

@DogCoffee我改变了[user saveInBackground];到[用户saveEventually];我仍然收到错误。就循环而言,你是指NSArray下的“for”语句? –

回答

2

的问题是,你是从关系中删除所有的对象:

NSArray *array = [myquery findObjects]; 
for (PFObject *object in array) 
{ 
    [relation removeObject:object]; 
} 

你的代码做打算,虽然你的阵列和去除的关系每个对象。

你想要做的是只删除该单元的作业。你可以得到使用indexPath

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    PFUser *user = [PFUser currentUser]; 
    PFRelation *relation = [user relationForKey:@"Favorites"]; 

    [relation removeObject:[self.objects objectAtIndex:indexPath.row]]; 

    [user saveInBackground]; 

    [self.tableView reloadData]; 
    [self loadObjects]; 
} 

你的第二个问题:

警告:被主线程上执行的长时间运行的操作。断开warnBlockingOperationOnMainThread()进行调试。

该警告是因为findObjects是同步呼叫。您应该改用findObjectsInBackground。但是,如果你做出上面给出的改变,你将不需要它。

+0

可能是一个愚蠢的问题,但我得到一个红色的错误指向[关系removeObject:[self.objects objectAtIndexPath:indexPath.row]]; 说,有'没有可见的'接口'NSArray'声明选择器'objectAtIndexPath'“ –

+1

对不起,我的坏。查看更新的答案。它应该是'objectAtIndex'而不是'objectAtIndexPath'。 – kRiZ

+0

哇,我不敢相信我一直在为“天”而努力,并且你在如此短的时间内解决了它!好样的!谢谢! –