PFQuery当前用户特定的对象

时间:2014-12-29 23:46:47

标签: ios parse-platform

希望有人可以帮我在iOS应用中使用Parse.com。

我有一个查询Parse.com类的表,该类名为Channel,包含5个主列。

  • 一:“objectId”
  • 二:“所有者”是指针< _User(主要类)>
  • 第三名:“名字”
  • 第四:“约”
  • 第五名:“形象”

    我只需要向表格查询当前用户特定的通道“名称”,“关于”和“图像”。

    在我的代码中,当我尝试查询whereKey:@“name”equalTo:[PFUser currentUser] .objectId]时,queryForTable方法不起作用(目前在下面评论)。所有列的当前ACL都是Public Read。

我目前的代码如下:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {


        self.parseClassName = @"Channel";

        //self.textKey = @"name";

        self.pullToRefreshEnabled = YES;

        self.paginationEnabled = YES;

        self.objectsPerPage = 5;
    }
    return self;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


#pragma mark - UIViewController

- (void)viewDidLoad {
    [super viewDidLoad];



}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    //self.canSearch = 0;

}

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

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

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



#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {//
//#warning Potentially incomplete method implementation.
// Return the number of sections.
//    return 0;
//}

//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
// Return the number of rows in the section.
//}





#pragma mark - PFQueryTableViewController



#pragma mark - Query

- (PFQuery *)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    //[query whereKey:@"name" equalTo:[PFUser currentUser].objectId];
    query.cachePolicy = kPFCachePolicyCacheThenNetwork;

    return query;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
    static NSString *CellIdentifier = @"Cell";

    MyChanTableViewCell *cell = (MyChanTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell
    PFFile *thumbnail = [object objectForKey:@"image"];
    PFImageView *thumbnailImageView = (PFImageView*)cell.showImage;
    thumbnailImageView.image = [UIImage imageNamed:@"Image.jpg"];
    thumbnailImageView.file = thumbnail;
    [thumbnailImageView loadInBackground];


    cell.mainTitle.text = [object objectForKey:@"name"];
    cell.detail.text = [object objectForKey:@"about"];


    return cell;


}


// Set CellForRowAtIndexPath


- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *LoadMoreCellIdentifier = @"LoadMoreCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LoadMoreCellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LoadMoreCellIdentifier];
    }
    return cell;
}

// Set TableView Height for Load Next Page
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if([self.objects count] == indexPath.row) {
        // Load More Cell Height
        return 60.0;
    } else {
        return 80.0;
    }
}

3 个答案:

答案 0 :(得分:1)

如果我理解正确的话,您想输出name当前用户所在的owner值:

[query whereKey:@"owner" equalTo:[PFUser currentUser]];

与指针比较时,您将比较完整对象。封面下的Parse SDK将其转换为正确比较objectId值和对象类型,您不必担心它。

答案 1 :(得分:0)

从您的评论中可以清楚地看出,您实际上并未尝试使用来查询名称;您尝试查询以便使用 objectID检索对象的名称​​。

在您当前的cellForRowAtIndexPath:方法中,您已经访问了PFObject的域名属性。您不需要(并且无法)指定您想要查询中的名称。查询将始终返回完整PFObjects。您只需指定要在其中搜索objectID的正确列...并且此特定列称为" objectID" (不是"名称"),例如:

- (PFQuery *)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    [query whereKey:@"objectID" equalTo:[PFUser currentUser].objectId];
    query.cachePolicy = kPFCachePolicyCacheThenNetwork;

    return query;
}

答案 2 :(得分:0)

 PFQuery *queryuser = [PFUser query];
    [queryuser whereKey:@"objectId" equalTo:[PFuser CurrentUser].objectId];
    [queryuser findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        NSString *name = [objects valueForKey:@"name"];
        NSLog(@"%@",name);




    }];

Hope this will work for you.