William Dayanayev
William Dayanayev

Reputation: 355

How to set self.parseClassName to a User Class?

I have to set a Parse Class Name for my PFQueryTableViewController, but I need to query all the users in my class. How would I be able to set self.parseClassName to a User Class? I tried:self.parseClassName = @"_User";, but that didn't work. Any ideas? I am using Xcode for this development. This is the error I get for using that class name.

2015-04-08 18:53:12.643 GPS Racing[76288:2673387] [Error]: bad characters in classname: (null) (Code: 103, Version: 1.6.4)

2015-04-08 18:53:14.948 GPS Racing[76288:2673404] [Error]: bad characters in classname: (null) (Code: 103, Version: 1.6.4) 2015-04-08 18:53:17.678 GPS Racing[76288:2673388] [Error]: bad characters in classname: (null) (Code: 103, Version: 1.6.4)

This is my full code. I am using storyboards, but I took this code from another app of mine that doesn't use storyboards.

    #import "FriendsViewController.h"

@interface FriendsViewController ()

@end

@implementation FriendsViewController

- (id)initWithCoder:(NSCoder *)aCoder {
    self = [super initWithCoder:aCoder];
    if (self) {
        // Customize the table

        // The className to query on
        self.parseClassName = @"TestClass";

        // The key of the PFObject to display in the label of the default cell style
        self.textKey = @"Ronen";

        // Uncomment the following line to specify the key of a PFFile on the PFObject to display in the imageView of the default cell style
        // self.imageKey = @"image";

        // 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 = 25;
    }
    return self;
}

// Override to customize what kind of query to perform on the class. The default is to query for
// all objects ordered by createdAt descending.
- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

    // 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 ([self.objects count] == 0) {
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }

    if (![PFUser currentUser]) {
        return nil;
    }
    // you can always query the user class like this

    [query orderByAscending:@"username"];

    return query;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.clearsSelectionOnViewWillAppear = NO;
        self.preferredContentSize = CGSizeMake(320.0, 600.0);
    }
}

- (void)viewDidLoad {

    [super viewDidLoad];
    NSLog (@"User ID:%@", [[PFUser currentUser] objectId]);

    PFQuery *query = [PFUser query];
    [query getObjectInBackgroundWithId:[[PFUser currentUser] objectId] block:^(PFObject *friends, NSError *error) {
        // Do something with the returned PFObject in the gameScore variable.

        NSArray *friendslist = [friends objectForKey:@"Friends"];

        // Do any additional setup after loading the view, typically from a nib.
    }];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

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

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

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (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 - Parse

- (void)objectsDidLoad:(NSError *)error {
    [super objectsDidLoad:error];

    // This method is called every time objects are loaded from Parse via the PFQuery
}

- (void)objectsWillLoad {
    [super objectsWillLoad];

    // This method is called before a PFQuery is fired to get more objects
}

// Override to customize the look of a cell representing an object. The default is to display
// a UITableViewCellStyleDefault style cell with the label being the first key in the object.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell
    cell.textLabel.text = [object objectForKey:@"quoteText"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Code: %@", [object objectForKey:@"by"]];
    UITableViewCell *showDetail = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.detailTextLabel.textColor = [UIColor whiteColor];

    return cell;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = @"Load more...";

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    cell.backgroundColor = [UIColor blueColor];
    cell.textColor = [UIColor whiteColor];

}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.objects.count;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

Upvotes: 0

Views: 614

Answers (3)

William Dayanayev
William Dayanayev

Reputation: 355

I figured it out. The real problem was that I was using code for a class without storyboards. Here is the correct code:

-  (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithClassName:@"_User"];
    self = [super initWithCoder:aDecoder];

    if (self) {
        // Custom the table

    // The className to query on
    self.parseClassName = @"_User";

    // The key of the PFObject to display in the label of the default cell `style`
    self.textKey = @"FN";

    // Uncomment the following line to specify the key of a PFFile on the PFObject to display in the imageView of the default cell style
    // self.imageKey = @"image";

    // 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 = 25;
  }
 return self;
}

- (PFQuery *)queryForTable {

    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

    // 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 ([self.objects count] == 0) {
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }

    [query orderByAscending:@"FN"];


    return query;

}

Don't forget to also set these parameters in the storyboard.

Upvotes: 1

rihekopo
rihekopo

Reputation: 3350

My experience is it works perfectly like this self.parseClassName = @"User";. If this version doesn't work please add more code, because it would be easier to help with more information.

In this case you don't use the self.parseClassName ivar in the queryForTable which returns the query for the PFQueryTableViewController.

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.parseClassName = @"User";
        self.pullToRefreshEnabled = YES;
        self.paginationEnabled = YES;
        self.objectsPerPage = 10;

    }
    return self;
}
- (PFQuery *)queryForTable {

    if (![PFUser currentUser]) {
        return nil;
    }
    // you can always query the user class like this
    PFQuery *query = [PFUser query];

    [query orderByAscending:@"username"];

    // or query another an class like this
    PFQuery *query2 = [PFQuery queryWithClassName:@"yourCustomClass"];

    return query;


}

Upvotes: 0

Jake T.
Jake T.

Reputation: 4378

To create a query for user objects in objective-c, try using PFQuery *query = [PFUser query];, then setting the query parameters (i.e. [query whereKey:@"playerName" notEqualTo:@"Michael Yabuti"]; )

More information found here: https://www.parse.com/docs/ios_guide#queries/iOS

Upvotes: 0

Related Questions