ios - PFRelation 从 PFRelation 中删除一个对象,而不是全部
<p><p>正在发生的事情的说明:用户在另一个 View 中添加了一项工作作为他们的最爱。现在,用户位于收藏夹选项卡中,并决定不再希望该工作成为他们的最爱之一,因此他们滑动以删除该工作。他们点击删除按钮并发生以下错误......代码按原样工作,但它也会删除用户保存为收藏夹的每一个作业,而不是只删除一个作业。 </p>
<p>我的代码还提醒我:</p>
<p><strong>警告:正在主线程上执行长时间运行的操作。
中断 warnBlockingOperationOnMainThread() 进行调试。</strong></p>
<pre><code>#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 = ;
if () {
// 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
{
;
}
- (void)viewWillAppear:(BOOL)animated {
;
}
- (void)objectsWillLoad {
;
}
- (void)viewDidAppear:(BOOL)animated {
;
;
}
- (void)viewWillDisappear:(BOOL)animated {
;
}
- (void)viewDidDisappear:(BOOL)animated {
;
}
- (void)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 = ;
if (cell == nil) {
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myJobsTableIdentifier];
}
// Configure the cell
PFFile *thumbnail = ;
PFImageView *thumbnailImageView = (PFImageView*);
thumbnailImageView.image = ;
thumbnailImageView.file = thumbnail;
;
UILabel *positionLabel = (UILabel*) ;
positionLabel.text = ;
UILabel *rotationLabel = (UILabel*) ;
rotationLabel.text = ;
UILabel *locationLabel = (UILabel*) ;
locationLabel.text = ;
UILabel *typeLabel = (UILabel*) ;
typeLabel.text = ;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if () {
NSIndexPath *indexPath = ;
Job *job = [ init];
JobDetailViewController *destViewController = segue.destinationViewController;
PFObject *object = ;
job.position = ;
job.poc = ;
job.email = ;
job.phone = ;
job.apply = ;
job.imageFile = ;
job.rotation = ;
job.location = ;
job.type = ;
job.clearance = ;
job.job_description = ;
job.qualifications = ;
job.originalJob = object;
destViewController.job = job;
}
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
;
if ( == indexPath.row) {
;
} else {
PFObject *photo = ;
NSLog(@"%@", photo);
// Do something you want after selected the cell
}
}
- (PFQuery *)queryForTable
{
PFUser *user = ;
PFRelation *relation = ;
PFQuery *myquery = ;
if (self.pullToRefreshEnabled) {
myquery.cachePolicy = kPFCachePolicyNetworkOnly;
}
return myquery;
}
#pragma mark - DeleteJobViewDelegate
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
PFUser *user = ;
PFRelation *relation = ;
PFQuery *myquery = ;
NSArray *array = ;
for (PFObject *object in array)
{
;
}
;
;
;
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>问题是您要从关系中删除所有对象:</p>
<pre><code>NSArray *array = ;
for (PFObject *object in array)
{
;
}
</code></pre>
<p>您的代码正在执行的操作是遍历您的数组并从关系中删除每个对象。</p>
<p>您要做的是仅删除该单元格的作业。您可以使用 <code>indexPath</code> 获得它:</p>
<pre><code>- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
PFUser *user = ;
PFRelation *relation = ;
];
;
;
;
}
</code></pre>
<p>你的第二个问题:</p>
<p><strong>警告:正在主线程上执行长时间运行的操作。中断 warnBlockingOperationOnMainThread() 进行调试。</strong></p>
<p>该警告是因为 <code>findObjects</code> 是一个同步调用。您应该改用 <code>findObjectsInBackground</code>。但如果你做出我上面给出的改变,你就不需要它了。</p></p>
<p style="font-size: 20px;">关于ios - PFRelation 从 PFRelation 中删除一个对象,而不是全部,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31152324/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31152324/
</a>
</p>
页:
[1]