• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - PFRelation 从 PFRelation 中删除一个对象,而不是全部

[复制链接]
菜鸟教程小白 发表于 2022-12-13 08:46:47 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

正在发生的事情的说明:用户在另一个 View 中添加了一项工作作为他们的最爱。现在,用户位于收藏夹选项卡中,并决定不再希望该工作成为他们的最爱之一,因此他们滑动以删除该工作。他们点击删除按钮并发生以下错误......代码按原样工作,但它也会删除用户保存为收藏夹的每一个作业,而不是只删除一个作业。

我的代码还提醒我:

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

#import "JobDetailViewController.h"
#import "MyFavoritesTableViewController.h"
#import "arse/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)initWithCoderNSCoder *)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 = @"osition";

        // 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)viewWillAppearBOOL)animated {
    [super viewWillAppear:animated];


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

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

    [self.tableView reloadData];

}

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

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

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

- (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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"osition"];
    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)prepareForSegueUIStoryboardSegue *)segue senderid)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"osition"];
        job.poc = [object objectForKey"OC"];
        job.email = [object objectForKey"Email"];
        job.phone = [object objectForKey:@"hone"];
        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 removeObjectbject];
        }

    [user saveInBackground];

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

    @end



Best Answer-推荐答案


问题是您要从关系中删除所有对象:

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

您的代码正在执行的操作是遍历您的数组并从关系中删除每个对象。

您要做的是仅删除该单元格的作业。您可以使用 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。但如果你做出我上面给出的改变,你就不需要它了。

关于ios - PFRelation 从 PFRelation 中删除一个对象,而不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31152324/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap