简体   繁体   中英

PFRelation Deleting One Object From A PFRelation, Not All

Explanation of what's taking place: The user has added a job as their favorite within another view. Now the user is in the Favorites tab and decides that they no longer want the job as one of their favorites anymore, so they swipe to delete the job. They tap the delete button and the errors below take place...the code works as is, but it also deletes every single job the user has saved as a favorite, instead of just deleting the one job.

My code also gives me an alert of:

Warning: A long-running operation is being executed on the main thread. Break on warnBlockingOperationOnMainThread() to debug.

#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

The problem is that you are removing all the objects from the Relation:

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

What your code is doing is going though your array and removing each object from the relation.

What you want to do is to delete the job for that cell only. You can get that using the 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];
}

Your second problem:

Warning: A long-running operation is being executed on the main thread. Break on warnBlockingOperationOnMainThread() to debug.

That warning is because findObjects is a synchronous call. You should use the findObjectsInBackground instead. But if you make the changes I gave above, you won't need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM