gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:14:30 25 4
gpt4 key购买 nike

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

我的代码还提醒我:

警告:正在主线程上执行长时间运行的操作。 中断 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

最佳答案

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

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。但是,如果您进行我上面给出的更改,则不需要它。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com