【问题标题】:Parse PFUser QueryWithPredicate Not working解析 PFUser QueryWithPredicate 不工作
【发布时间】:2015-08-29 05:48:40
【问题描述】:

我正在尝试实现用户的搜索功能,并且我正在尝试使用 NSPredicate 但它不起作用。

我正在尝试使用 LIKE 查询。

这是我正在使用的代码

NSPredicate *userPredicate = [NSPredicate predicateWithFormat:@"(%@ CONTAINS[cd] %@) OR (%@ CONTAINS[cd] %@) AND %@ != %@",KEY_FirstName, sender.text,KEY_Lastname, sender.text,KEY_Id,[PFUser currentUser].objectId];
queryForUserSearch = [PFUser queryWithPredicate:userPredicate];

// I also tried this 
NSPredicate *userPredicate = [NSPredicate predicateWithFormat:@"(%@ = %@) OR (%@ = %@) AND %@ != %@",KEY_FirstName, sender.text,KEY_Lastname, sender.text,KEY_Id,[PFUser currentUser].objectId];
queryForUserSearch = [PFUser queryWithPredicate:userPredicate];

我遇到了错误

原因:'[PFQuery queryWithClassName:predicate:] 不支持正则表达式查询。请尝试构建您的数据,以便您可以使用 equalTo 或 containsIn 查询。'

我也在搜索这个,但对我没有用。

【问题讨论】:

  • 您将不得不使用各种持有者对象来首先存储来自 PFuser 数据的数据并从该对象中提取对象。这通常使用 nsobject 的子类来完成。
  • 我可以给你看一个你喜欢的例子
  • 如果您能向我们展示这方面的示例,将会非常有帮助。
  • 给我 30 分钟,生病的帖子
  • Malav,你去吧,这就是你的做法,这是你如何从 Parse 中提取数据并使用它的示例

标签: ios objective-c parse-platform pfquery pfuser


【解决方案1】:

首先,你的对象,这是你设置它的方式,一个例子:

书.h

#import <Foundation/Foundation.h>

@interface Book : NSObject

@property (strong, nonatomic)NSString *bookID;
@property (strong, nonatomic)NSString *publishingYear;
@property (strong, nonatomic)NSString *author;
@property (strong, nonatomic)NSString *printHouse;
@property (strong, nonatomic)NSString *title;
@property (strong, nonatomic)NSString *ISBN;

-(id)initWithBookObjectId:(NSString *)bookID
           publishingYear:(NSString *)publishingYear
                   author:(NSString *)author
               printHouse:(NSString *)printHouse
                    title:(NSString *)title
                     ISBN:(NSString *)ISBN;

- (id)initWithDictionary:(PFObject *)dic;

@end

书.m

#import "Book.h"
#define NSHNullCheck(object) ([object isKindOfClass:[NSNull class]] ? nil : object)

@implementation Book
{
    NSDictionary * descriptionDict;
}

-(id)initWithBookObjectId:(NSString *)bookID
           publishingYear:(NSString *)publishingYear
                   author:(NSString *)author
               printHouse:(NSString *)printHouse
                    title:(NSString *)title
                     ISBN:(NSString *)ISBN;

{
    self = [super init];
    if (self) {
        _bookID = bookID;
        _publishingYear = publishingYear;
        _author = author;
        _printHouse = printHouse;
        _title = title;
        _ISBN = ISBN;}
    return self;
}

- (id)initWithDictionary:(PFObject *)dic
{
    self = [self initWithBookObjectId:dic.objectId
                       publishingYear:NSHNullCheck([dic  valueForKey:@"publishingYear"])
                               author:NSHNullCheck([dic  valueForKey:@"author"])
                           printHouse:NSHNullCheck([dic  valueForKey:@"printHouse"])
                                title:NSHNullCheck([dic  valueForKey:@"title"])
                                 ISBN:NSHNullCheck([dic  valueForKey:@"ISBN"])];


    descriptionDict = @{ @"sessionObjectId":_bookID,
                         @"teacherAge":_publishingYear,
                         @"teacherEmail":_author,
                         @"teacherFacebookuniquekey":_printHouse,
                         @"teacherFirstname":_title,
                         @"teacherGender":_ISBN};
    return self;
}

- (id)init
{
    self = [self initWithBookObjectId:nil
                       publishingYear:nil
                               author:nil
                           printHouse:nil
                                title:nil
                                ISBN:nil];
    return self;
}

- (NSString *)description
{
    return descriptionDict.description;
}

@end

在对象模型中存储数据: 书籍是一个包含您的“书籍对象”的数组 无论您使用 Parse 的查询从网络中提取数据的任何位置,以下 sn-p 代码都会进入。

for (PFObject *object in objects) { //"objects" here is the NSArrray returned from the parse query!
    Book *book = [[Book alloc] initWithBookObjectId:object.objectId];
    book.publishingYear = object[@"publishingYear"];
    book.author = object[@"author"];
    book.printHouse = object[@"printHouse"];
    book.title = object[@"title"];
    book.ISBN = object[@"isbn"];
    [self.books addObject:book];
}
if ([self.searchTerm isEqualToString:@""]) {
    self.filteredBooksArray = self.books;
} else {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.title contains[c] %@",self.searchTerm];
    self.filteredBooksArray = [NSMutableArray arrayWithArray:[self.books filteredArrayUsingPredicate:predicate]];
}
[self.booksTable reloadData];

因此,这就是您使用 PFObjects 进行搜索的方式,您首先需要提取数据,将此数据保存在 NSObject 子类(数据对象模型)中,然后像在任何对象模型上一样使用正则表达式谓词进行搜索IOS中的功能。

有这个方法:

PFQuery *query = [PFQuery queryWithClassName:@"Post"]
[query whereKey:@"hashtags" containsAllObjectsInArray:@[@"#parse", @"#ftw"]];
NSArray *parseFTWPosts = [query findObjects];

然后这个方法:

/ Using PFQuery
 [query whereKey:@"playerName" notEqualTo:@"Michael Yabuti"];
 [query whereKey:@"playerAge" greaterThan:@18];

 // Using NSPredicate
 NSPredicate *predicate = [NSPredicate predicateWithFormat: @"playerName != 'Michael Yabuti' AND playerAge > 18"];
 PFQuery *query = [PFQuery queryWithClassName:@"GameScore" predicate:predicate];

然后这个sn-p:

Specifying Constraints with NSPredicate

To get the most out of PFQuery we recommend using its methods listed below to add constraints. However, if you prefer using NSPredicate, a subset of the constraints can be specified by providing an NSPredicate when creating your PFQuery.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"playerName = 'Dan Stemkosk'"];
PFQuery *query = [PFQuery queryWithClassName:@"GameScore" predicate:predicate];

let predicate = NSPredicate(format: "playerName = 'Dan Stemkosk'")
var query = PFQuery(className: "GameScore", predicate: predicate)

These features are supported:

    Simple comparisons such as =, !=, <, >, <=, >=, and BETWEEN with a key and a constant.
    Containment predicates, such as x IN {1, 2, 3}.
    Key-existence predicates, such as x IN SELF.
    BEGINSWITH expressions.
    Compound predicates with AND, OR, and NOT.
    Sub-queries with "key IN %@", subquery.

The following types of predicates are not supported:

    Aggregate operations, such as ANY, SOME, ALL, or NONE.
    Regular expressions, such as LIKE, MATCHES, CONTAINS, or ENDSWITH.
    Predicates comparing one key to another.
    Complex predicates with many ORed clauses.

这里还有更多:

https://github.com/ParsePlatform/Docs/blob/master/en/ios/queries.mdown

【讨论】:

  • 我想这在您本地过滤数据时很有用我想做的只是从满足谓词条件的解析中获取数据。
  • 哦,是的,这是不行的,你不能用解析来做到这一点,你只能使用他们的查询方法并在查询流中使用“whereKeyEquals”等提取所有数据
  • Parse 提供了方法 [PFUser queryWithPredicate:userPredicate] 所以我想一定有什么方法可以使用它。
  • 但是,我也会发布这个,因为这可能会有所帮助
  • 所以,这就是你的意思,是的,你可以使用谓词,但它受到限制,所以我以多种方式发布了本地和服务器搜索的答案,基本上是解析允许的唯一方式'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多