Using pointers for a "like" relationship in parse - ios

I'm am fairly new to ios programming and to parse, so if i am not explaining something clearly ill be more than happy to try my best to expand on this.
In parse i have three tables User Activity and post the code i have now works well for a "following" relationship but does not for a "like" relationship, i want to take the object id from the Post table and the object id of the user that is liking the post from the User table, and insert them in to the Activity Table. Any help will be appreciated, Thank you in advanced.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == self.objects.count && self.paginationEnabled) {
[self loadNextPage];
}
}
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query includeKey:#"User"];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
[query whereKey:#"filter" hasPrefix:#"90"];
[query orderByDescending:#"createdAt"];
return query;
}
- (void)likeButton:(LikeButton *)button didTapWithSectionIndex:(NSInteger)index {
PFObject *post = [self.objects objectAtIndex:index];
[post fetchIfNeeded];
PFUser *user = post[#"User"];
if (!button.selected) {
[self likePost:user];
}
else {
[self unlikePost:user];
}
[self.tableView reloadData];
}
- (void)likePost:(PFUser *)user {
if (![user.objectId isEqualToString:[PFUser currentUser].objectId]) {
[self.likeArray addObject:user.objectId];
PFObject *likeActivity = [PFObject objectWithClassName:#"Activity"];
likeActivity[#"fromUser"] = [PFUser currentUser];
likeActivity[#"toPost"] = user;
likeActivity[#"type"] = #"Like";
[likeActivity saveEventually];
}
}
- (void)unlikePost:(PFUser *)user {
[self.likeArray removeObject:user.objectId];
PFQuery *query = [PFQuery queryWithClassName:#"Activity"];
[query whereKey:#"fromUser" equalTo:[PFUser currentUser]];
[query whereKey:#"toPost" equalTo:user];
[query whereKey:#"type" equalTo:#"like"];
[query findObjectsInBackgroundWithBlock:^(NSArray *likeActivities, NSError *error) {
if (!error) {
for (PFObject *likeActivity in likeActivities) {
[likeActivity deleteEventually];
}
}
}];
}

OK, first off I would mention that you should be very careful with case-sensitivity. String matches in Parse are case-sensitive so if you create it with type:#"Like" and then try to find it with whereKey:#"type" equalTo:#"like" you won't get a match!
Now, as for your code, you want to link the current user to the selected post right?
If so here's a sample of what you should do instead:
- (void)likeButton:(LikeButton *)button didTapWithSectionIndex:(NSInteger)index {
PFObject *post = [self.objects objectAtIndex:index];
if (!button.selected) {
[self likePost:post];
}
else {
[self unlikePost:post];
}
- (void)likePost:(PFObject *)post {
[self.likeArray addObject:user.objectId];
PFObject *likeActivity = [PFObject objectWithClassName:#"Activity"];
likeActivity[#"fromUser"] = [PFUser currentUser];
likeActivity[#"toPost"] = post;
likeActivity[#"type"] = #"like";
[likeActivity saveEventually];
}
Looks like your code was copy-pasted from your "follow" logic but not updated to reflect the fact that you're now doing a "like", so I've fixed that for you. You'll need to make similar changes to your unlikePost: method too.

Related

Why can't I find the second user when joining a chat using Parse?

When I search for a chat it successfully loads the chatview controller but it fails to find another user, so its just one user in a chat. Maybe I'm referencing the second user wrongly. Not sure how to correct it though.
Code:
- (void)actionChat:(NSString *)groupId
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
ChatView *chatView = [[ChatView alloc] initWith:groupId];
// chatView.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:chatView animated:YES]; // This JSQMessageViewController
}
- (IBAction)startChat:(id)sender { // The button
PFQuery *query = [PFUser query];
[query whereKey:#"objectId" notEqualTo:[PFUser currentUser].objectId];
[query setSkip:arc4random()%2];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!object) {
NSLog(#"The getFirstObject request failed.");
} else {
//You now have a random user from your Database, do what you want with it.
PFUser *user1 = [PFUser currentUser];
NSString *groupId = StartPrivateChat(user1,object);
[self actionChat:groupId];
}
}];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
NSString* StartPrivateChat(PFUser *user1, PFUser *user2)
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
NSString *id1 = user1.objectId;
NSString *id2 = user2.objectId;
//---------------------------------------------------------------------------------------------------------------------------------------------
NSString *groupId = ([id1 compare:id2] < 0) ? [NSString stringWithFormat:#"%#%#", id1, id2] : [NSString stringWithFormat:#"%#%#", id2, id1];
//---------------------------------------------------------------------------------------------------------------------------------------------
NSArray *members = #[user1.objectId, user2.objectId];
//---------------------------------------------------------------------------------------------------------------------------------------------
// CreateRecentItem(user1, groupId, members, user2[PF_USER_FULLNAME]);
// CreateRecentItem(user2, groupId, members, user1[PF_USER_FULLNAME]);
//---------------------------------------------------------------------------------------------------------------------------------------------
return groupId;
}

Set selected row in UIPickerView from Parse objects

In a view there is a text field and a picker view. The picker view items are loaded from a Parse query.
The text field text is always one of the picker view items.
The picker view selected item must be the same as the text field text. That is my code for the moment, but it throws an exception:
PFQuery *query = [PFQuery queryWithClassName:#"floors"];
[query whereKey:#"floor_restaurant" equalTo:self.restaurante.objectId];
[query orderByDescending:#"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
_pickerData = objects;
for (int i = 0; i < [objects count]; i++)
{
if ([[objects objectAtIndex:i] isEqualToString: self.floor_name_text.text]){
[self.floor_picker selectRow:i inComponent:0 animated:YES];
break;
}
}
[self.floor_picker reloadAllComponents];
}
else {
NSLog(#"error");
}
}];
That is the exception:
2015-02-28 21:03:20.707 RestAppXXI[675:60b] -[PFObject isEqualToString:]: unrecognized selector sent to instance
I am new to Parse with iOS and any help is welcome.
EDITED :
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
PFObject *object = _pickerData[row];
return object[#"floor_name"];
}
Thanks to the comments of rmaddy and a little searching, I have resolved my issue, here is the final code that works:
[PFQuery *query = [PFQuery queryWithClassName:#"floors"];
[query whereKey:#"floor_restaurant" equalTo:self.restaurante.objectId];
[query orderByDescending:#"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
_pickerData = objects;
for (int i = 0; i < [objects count]; i++)
{
NSString *textoactual = self.floor_name_text.text;
if ([[[objects objectAtIndex:i] objectForKey:#"floor_name" ] isEqualToString: textoactual]){
[self.floor_picker reloadAllComponents];
[self.floor_picker selectRow:i inComponent:0 animated:YES];
break;
}
}
}
else {
NSLog(#"error");
}
}];

Search more than 1000 queries in parse iOS

I have a parse iOS app where I need to search the names of a couple thousand users (~3,000). I am trying to modify my search code so that I can do this but I need help. Right now my code for search looks like this:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
return self.objects.count;
} else {
return self.searchResults.count;
}
}
-(void)filterResults:(NSString *)searchTerm :(int)limit :(int)skip {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName:#"_User"];
[query whereKey:#"isTeacher" equalTo:#"False"];
[query setLimit: limit];
[query setSkip: skip];
[[[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (objects.count == limit) {
[self performTeacherQueryWithLimit:limit andSkip:skip+limit];
}
else{
NSArray *results = [NSArray arrayWithArray:objects];
NSLog(#"%#", results);
NSLog(#"%lu", (unsigned long)results.count);
NSLog(#"results^");
[self.searchResults addObjectsFromArray:results];
NSPredicate *searchPredicate =
[NSPredicate predicateWithFormat:#"SELF.name contains[c] %#",searchTerm];
_searchResults = [NSMutableArray arrayWithArray:[results filteredArrayUsingPredicate:searchPredicate]];
[self.searchDisplayController.searchResultsTableView reloadData];
NSLog(#"%#", _searchResults);
NSLog(#"%lu", (unsigned long)_searchResults.count);
NSLog(#"search results^");
}
}];
]]
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterResults:searchString];
return YES;
}
This code will not work because I call the filterResults at the bottom without all the correct parameters but that is because I got halfway through and now I am stuck. I know I need to use the setSkip but I'm not sure how to make that work for my searching. Any help would be awesome! Thanks!
I do this sort of thing with a method that handles the query and its results together, like this:
- (void)runQuery:(PFQuery *)query filling:(NSMutableArray *)array completion:(void (^)(BOOL))completion {
query.skip = array.count;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
[array addObjectsFromArray:objects];
if (objects.count < query.limit) {
return completion(YES);
} else {
[self runQuery:query filling:array completion:completion];
}
} else {
return completion(NO);
}
}];
}
Use it like this:
PFQuery *query = [PFQuery queryWithClassName:#"MyClass"];
// setup query
query.limit = // set this to a reasonable size
// the given method will do ceil(N / limit) finds, where N is the number
// of rows that satisfy the query
NSMutableArray *array = [#[] mutableCopy];
[self runQuery:query filling:array completion:^(BOOL success) {
NSLog(#"%#", array);
// you would do your local search and set search results here
}];
A more functional version would call a progress block in between queries. This would allow you to continuously update results. For that, just add a progress block parameter and call it right after [array addObjectsFromArray:objects];.

Return a query from inside a block

i hope NSlog outside of block can have value.
i don't know how to fix it. i hope loadPuppiesFromJSON can working.
could somebody help me!?THX!!
- (id)init
{
if((self = [super init]))
{
allPuppies=[self loadPuppiesFromJSON];
}
return self;
}
- (NSArray *)loadPuppiesFromJSON
{
PFQuery *query = [PFQuery queryWithClassName:#"Information"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
// Do something with the found objects
for (PFObject *object in objects) {
Fish *fish = [[Fish alloc]init];
fish.price = object[#"price"];
fish.name = object[#"name"];
[object saveInBackground];
self.allFishes = objects;
NSLog(#"%#",allFishes);<-----here have some value
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
NSLog(#"%#",allFishes);<----here doesn't have value
}
The reason your NSLog(#"%#",allFishes); doesn't have a value is that it is being called before your query is complete. It's a Race Condition, and at run-time here is what the program thinks you want:
Start query
Call NSLog outside the block - no data yet, so it's empty
Call completion block when it is ready - this is when the data is generated so you get a populated NSLog here
Since you also want to have the value return, try this instead - just remove your NSLost from outside the block and call a second function with the return value to continue your logic:
- (void)loadPuppiesFromJSON
{
PFQuery *query = [PFQuery queryWithClassName:#"Information"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
// Do something with the found objects
for (PFObject *object in objects) {
Fish *fish = [[Fish alloc]init];
fish.price = object[#"price"];
fish.name = object[#"name"];
[object saveInBackground];
}
self.allFishes = objects;
NSLog(#"%#",allFishes);<-----here have some value
[self processPuppiesArray:objects];
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
//No NSLog out here - it won't do anything
}
- (void)processPuppiesArray:(NSArray *)puppiesArray
{
//Continue processing here...
}
The NSLog where you are not getting any value is because block is called in background after it completes your function execution. Thats why before executing you block, it finishes your function execution and during that time allFishes variable is empty/nil. Once the block excution starts and assigns value to allFishes after that this variable gets the value.
If you need to execute some code after allFishes have value create a function and call it after blockexecution finishes like below:
- (NSArray *)loadPuppiesFromJSON
{
PFQuery *query = [PFQuery queryWithClassName:#"Information"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
// Do something with the found objects
for (PFObject *object in objects) {
Fish *fish = [[Fish alloc]init];
fish.price = object[#"price"];
fish.name = object[#"name"];
[object saveInBackground];
self.allFishes = objects;
NSLog(#"%#",allFishes);<-----here have some value
[self dataLoadFinished];
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
NSLog(#"%#",allFishes);<----here doesn't have value
}
-(void) dataLoadFinished {
NSLog(#"%#",self.allFishes);<----here you will have value
}
The reason you aren't retrieving any data is because you are trying to retrieve an Array from a function but never return anything.
When you use the -(NSArray *)loadPuppiesFromJSON you actually have to return an Array which you are not doing.
For example:
-(NSArray *)loadPuppiesFromJson {
NSArray *allFishes;
allFishes = #[#"someinfo1",#"someinfo2",#"someinfo3"];
return allFishes; // this is where your getting your error and need for it to return anything
}
I would suggest butting your code into a void function like so:
-(void)loadPuppiesFromJSON {
PFQuery *query = [PFQuery queryWithClassName:#"Information"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.allFishes = objects;
for (PFObject *object in objects) {
Fish *fish = [[Fish alloc]init];
fish.price = object[#"price"];
fish.name = object[#"name"];
[object saveInBackground];
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
NSLog(#"%#",allFishes);<----here has value now
}
And call it in viewDidLoad or viewDidAppear or wherever you need it with:
[self loadPuppiesFromJSON]

New Data save to Mutable Array in Parse not calling save function

I have a module that saves data based on user inputs from text fields and adds it to an array saved in Parse. Testing with pre-populated data the module works fine. However, if I go to add data where there previously wasn't any it causes the app to ignore the saving element all together. What is the best way to add new arrays to Parse where no previous data existed? Here is the existing code:
- (IBAction)saveButton:(id)sender
{
[getData showGlobalProgressHUDWithTitle:#"Saving Test"];
PFQuery *tankQuery = [PFQuery queryWithClassName:#"WaterTests"];
[tankQuery whereKey:#"tankObjectId" equalTo:_passedValue];
[tankQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error)
{
for (PFObject *object in objects)
{
calciumArray = [object valueForKey:#"calciumArray"];
nitrateArray = [object valueForKey:#"nitrateArray"];
phosArray = [object valueForKey:#"phosphateArray"];
salinityArray = [object valueForKey:#"salinityArray"];
alkArray = [object valueForKey:#"alkArray"];
phArray = [object valueForKey:#"phArray"];
tempArray = [object valueForKey:#"tempArray"];
[calciumArray addObject: addCalcium.text];
[nitrateArray addObject: addNitrate.text];
[phosArray addObject: addPhosphate.text];
[salinityArray addObject: addSalinity.text];
[alkArray addObject: addAlk.text];
[phArray addObject: addPH.text];
[tempArray addObject: addTemp.text];
[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
{
NSLog(#"Something happened");
[getData dismissGlobalHUD];
[self hideTestView];
}
else
{
NSLog(#"Nothing happened: %#", error);
}
[self resignFirstResponder];
[_chartView reloadData];
}];
}
}
}];
}
I know that I'm close, but don't quite understand why it's not firing off the saving code. I believe its because I'm trying to retrieve objects that are empty first, but I could be wrong about that. Anyone have any advice for this?
UPDATE Here is the ViewDidLoad Code as it pertains to retrieving the data.
- (void)viewDidLoad
{
self.title = #"Water Quality";
statQuery = [PFQuery queryWithClassName:#"WaterTests"];
[statQuery whereKey:#"tankObjectId" equalTo:_passedValue];
[statQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error)
{
for (PFObject *object in objects)
{
[statQuery whereKey:#"tankObjectId" equalTo:_passedValue];
calciumArray = [object valueForKey:#"calciumArray"];
_footerView.sectionCount = calciumArray.count;
NSLog(#"CALCIUM ARRAY: %#", calciumArray);
nitrateArray = [object valueForKey:#"nitrateArray"];
NSLog(#"NITRATE ARRAY: %#", nitrateArray);
phosArray = [object valueForKey:#"phosphateArray"];
NSLog(#"PHOSPHATE ARRAY: %#", phosArray);
salinityArray = [object valueForKey:#"salinityArray"];
NSLog(#"SALINITY ARRAY: %#", salinityArray);
alkArray = [object valueForKey:#"alkArray"];
NSLog(#"ALKALINITY ARRAY: %#", alkArray);
phArray = [object valueForKey:#"phArray"];
NSLog(#"PH ARRAY: %#", phArray);
tempArray = [object valueForKey:#"tempArray"];
NSLog(#"TEMPERATURE ARRAY: %#", tempArray);
}
}
if (calciumArray == nil || [calciumArray count] == 0)
{
NSLog(#"You should probably fire off the new test function here.");
[_chartView setUserInteractionEnabled:NO];
[self newTest:self];
NSLog(#"Error: %#", error);
}
}];
This is the only other time the data is mentioned at all in the view. I allocate and initialize the arrays elsewhere, but I don't think that's the problem considering existing arrays pull and save just fine.
(PFObject *object in objects)
{
calciumArray = [object valueForKey:#"calciumArray"];
.
.
. //Other object for array
[arrayMu] [calciumArray addObject: addCalcium.text]; //Add in NSMutableArray
.
.
.
[objects save]; //Instead of saveinbackground.
}
Try this out. Also do reply back in time as after a long time I had would have to read all your query again as not in touch with it.
UPADTE...
-(IBAction)updateButton:(id)sender
{
PFQuery *query = [PFQuery queryWithClassName:#"UserDetail"];
NSString *str =self.nameTextField.text;
[query whereKey:#"name" containsString:str];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error)
{
if (!error)
{
NSLog(#"Successfully retrieved: %#", [object objectId]); //I retrieve the object succesfully
object[#"job"] = self.jobTextField.text; //Made changes to it.
object[#"hobby"] = self.hobbyTextField.text;
[object saveInBackground]; //Again saved it in background
}
else
{
NSLog(#"Error: %#", [error localizedDescription]);
} }];
}
Then I checked my parse db and my data was modified.
I was able to properly add a new array from a single object if nothing else was available by plainly adding a new object to Parse:
- (IBAction)saveButton:(id)sender
{
[getData showGlobalProgressHUDWithTitle:#"Saving Test"];
PFQuery *tankQuery = [PFQuery queryWithClassName:#"WaterTests"];
[tankQuery whereKey:#"tankObjectId" equalTo:_passedValue];
PFObject *testObject = [[PFObject alloc] initWithClassName:#"WaterTests"];
[tankQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (objects.count == 0)
{
[testObject setObject:_passedValue forKey:#"tankObjectId"];
[testObject addObject:addCalcium.text forKey:#"calciumArray"];
[testObject addObject:addNitrate.text forKey:#"nitrateArray"];
[testObject addObject:addPhosphate.text forKey:#"phosphateArray"];
[testObject addObject:addSalinity.text forKey:#"salinityArray"];
[testObject addObject:addAlk.text forKey:#"alkArray"];
[testObject addObject:addPH.text forKey:#"phArray"];
[testObject addObject:addTemp.text forKey:#"tempArray"];
[testObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
{
[getData dismissGlobalHUD];
[self hideTestView];
NSLog(#"Something, somewhere, saved.");
}
else
{
NSLog(#"HOORAY ERROR: %#", error);
}
[_chartView reloadData];
}];
}
However, I also had to check to see if there were any objects to begin with. If there were not any, simply add them in new. If there were, I had to run the code to query the database and add a for loop for every object that was found with the given criteria:
else if (objects.count != 0)
{
for (PFObject *object in objects)
{
[object addObject:addCalcium.text forKey:#"calciumArray"];
[object addObject:addNitrate.text forKey:#"nitrateArray"];
[object addObject:addPhosphate.text forKey:#"phosphateArray"];
[object addObject:addSalinity.text forKey:#"salinityArray"];
[object addObject:addAlk.text forKey:#"alkArray"];
[object addObject:addPH.text forKey:#"phArray"];
[object addObject:addTemp.text forKey:#"tempArray"];
[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
if (!error)
{
[getData dismissGlobalHUD];
[self hideTestView];
[_chartView reloadData];
NSLog(#"Here we go again");
}
}];
}
}
}];
}
I'm submitting this as my own answer and offering this to the community wiki to help others figure out similar problems. Problem: SOLVED!

Resources