我正在使用Parse.com
作为我应用的后端。
每个用户都可以访问PFObjects
列表。如果当前用户想要“喜欢”一个对象,我想将其从当前用户的主列表中删除。
以下是我的代码,在ObjectsDidLoad
中运行。
如果我退出以下代码,PFObject
中的PFRelation
即被删除,但UITableView
NSMutableArray
中的最后一项正在删除除去。
我在错误的地方运行吗?
也许错过了一步?
谢谢!
以下是代码:
- (void)objectsDidLoad:(NSError *)error {
[super objectsDidLoad:error];
PFRelation *relation = [[PFUser currentUser] relationForKey:@"myRelation"];
PFQuery *relationQuery = relation.query;
[relationQuery findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
NSMutableArray *arrayToShow = [NSMutableArray arrayWithArray:self.objects];
for (PFObject *object1 in self.objects) {
for (PFObject *object2 in results) {
if ([[object1 objectForKey:@"title"] isEqualToString:[object2 objectForKey:@"title"]]) {
[arrayToShow removeObject:object1];
}
}
}
self.objectsToShowArray = [NSMutableArray arrayWithArray:arrayToShow];
[self.tableView reloadData];
}];
}
更多说明:
表A或主表加载Parse类中的所有对象。每个用户都可以访问它。
用户选择一个对象转到另一个视图控制器,可以点击一个按钮将其添加到另一个Parse类。这也会创建PFRelation。
因此,如果用户将对象'xyz'添加到Parse Class B和Table B,我希望该对象不在表A中显示(尽管它必须保留在Parse Class A中,因为每个用户都可以访问它)
所以,我想查询Parse Class A,查询PFRelation,并从表A中显示的数组中删除匹配的对象。
这是我的queryForTable方法:
- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query orderByAscending:@"title"];
// **********************************
// PFRelation
PFRelation *relation = [[PFUser currentUser] relationForKey:@"myRelation"];
PFQuery *relationQuery = relation.query;
[relationQuery findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
NSMutableArray *arrayToShow = [NSMutableArray arrayWithArray:self.objects];
for (PFObject *object1 in self.objects) {
for (PFObject *object2 in results) {
if ([[object1 objectForKey:@"title"] isEqualToString:[object2 objectForKey:@"title"]]) {
[arrayToShow removeObject:object1];
}
}
}
self.objectsToShowArray = [NSMutableArray arrayWithArray:arrayToShow];
[self.tableView reloadData];
}];
// **********************************
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSObject * object = [prefs objectForKey:@"category"];
if(object != nil){
//object is there
self.categoryString = [NSString stringWithFormat:@"%@", object];
[query whereKey:@"category" equalTo:self.categoryString];
}
// A pull-to-refresh should always trigger a network request.
[query setCachePolicy:kPFCachePolicyNetworkOnly];
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
//
// If there is no network connection, we will hit the cache first.
if (self.objects.count == 0 || ![[UIApplication sharedApplication].delegate performSelector:@selector(isParseReachable)]) {
[query setCachePolicy:kPFCachePolicyCacheThenNetwork];
}
return query;
}
另一次尝试:
- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query orderByAscending:@"title"];
// **********************************
// PFRelation Query
PFRelation *relation = [[PFUser currentUser] relationForKey:@"myRelation"];
PFQuery *relationQuery = relation.query;
[relationQuery findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
PFQuery *theQuery = [PFQuery queryWithClassName:self.parseClassName];
[theQuery whereKey:@"title" matchesKey:@"title" inQuery:relationQuery];
[theQuery findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
NSMutableArray *arrayToShow = [NSMutableArray arrayWithArray:self.objects];
for (PFObject *object1 in self.objects) {
for (PFObject *object2 in objects) {
if ([[object1 objectForKey:@"title"] isEqualToString:[object2 objectForKey:@"title"]]) {
[arrayToShow removeObject:object1];
}
}
}
self.objectsToShowArray = [NSMutableArray arrayWithArray:arrayToShow];
[self.tableView reloadData];
}];
}];
// **********************************
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSObject * object = [prefs objectForKey:@"category"];
if(object != nil){
//object is there
self.categoryString = [NSString stringWithFormat:@"%@", object];
[query whereKey:@"category" equalTo:self.categoryString];
}
// A pull-to-refresh should always trigger a network request.
[query setCachePolicy:kPFCachePolicyNetworkOnly];
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
//
// If there is no network connection, we will hit the cache first.
if (self.objectsToShowArray.count == 0 || ![[UIApplication sharedApplication].delegate performSelector:@selector(isParseReachable)]) {
[query setCachePolicy:kPFCachePolicyCacheThenNetwork];
}
return query;
}
相同的结果。在日志中,它删除了正确的PFObject
,但在UITableView
中删除了最后一个对象。我在俯瞰什么?
越来越近了
我快到了。我想出了获得我想要的结果的正确方法,但现在我需要在查询中添加另一层。这是我的代码:
PFRelation *relation = [[PFUser currentUser] relationForKey:@"myRelation"];
PFQuery *relationQuery = relation.query;
PFRelation *adoptedRelation = [[PFUser currentUser] relationForKey:@"mySecondRelation"];
PFQuery *adoptedRelationQuery = adoptedRelation.query;
PFQuery *matchQueryOne = [PFQuery queryWithClassName:self.parseClassName];
[matchQueryOne whereKey:@"title" matchesKey:@"title" inQuery:relationQuery];
PFQuery *matchQueryTwo = [PFQuery queryWithClassName:self.parseClassName];
[matchQueryTwo whereKey:@"title" matchesKey:@"title" inQuery:adoptedRelationQuery];
PFQuery *queryOne = [PFQuery queryWithClassName:self.parseClassName];
[queryOne whereKey:@"title" doesNotMatchKey:@"title" inQuery:matchQueryOne];
[queryOne whereKey:@"title" doesNotMatchKey:@"title" inQuery:matchQueryTwo];
[queryOne orderByAscending:@"title"];
我需要运行两个关系并合并结果,然后获得输出。
在这个例子中,我的queryOne中只有第二个案例被命中。
答案 0 :(得分:0)
PFQueryTableViewController
维护要从您提供的查询中显示的项目列表。您无法使用大多数逻辑PFQueryTableViewController
为您提供不同的查询并过滤这些结果。
更新您提供的查询PFQueryTableViewController
以包含限制项目返回的条款,这样您就不需要在本地过滤。