populate label with data from different ViewController - ios

So what I have is a pop being activated that has its own ViewController and XIB file. It has a label called #property (strong, nonatomic) IBOutlet UILabel *addressLabel; the issue is I am trying to call that label in the first VC (where the pop up is called) since that is where I query the database to populate the label with a name from the DB. Can I make *addressLabel global so it is recongnized in the main VC?
Here is the main VC where the pop is called
- (void)receivedSighting:(FYXVisit *)visit updateTime:(NSDate *)updateTime RSSI:(NSNumber *)RSSI;
{
NSLog(#"Gimbal Beacon!!! %#", visit.transmitter.name);
// this will be invoked when an authorized transmitter is sighted during an on-going visit
[self showTransmittersView];
//This gets the popup
SamplePopupViewController *samplePopupViewController = [[SamplePopupViewController alloc] initWithNibName:#"SamplePopupViewController" bundle:nil];
[self presentPopupViewController:samplePopupViewController animated:YES completion:nil];
PFQuery *query = [PFQuery queryWithClassName:#"houses"];
[query whereKey:#"name" equalTo:[NSString stringWithFormat:#"%#",visit.transmitter.name]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
NSLog(#"address: %#", [object objectForKey:#"address"]);
NSLog(#"url:: %#", [object objectForKey:#"url"]);
NSString *displayAddress = [NSString stringWithFormat:#"%#", [object objectForKey:#"address"]];
NSString *displayDescription = [NSString stringWithFormat:#"%#", [object objectForKey:#"description"]];
NSString *displayUrl = [NSString stringWithFormat:#"%#", [object objectForKey:#"url"]];
//This is where I was getting the info to put in the pop up label
self.addressLabel.text = displayAddress;
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}

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;
}

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]

SVProgressHUD how to show in next ViewController

I am having a TableViewController with some items and then having a filterViewController, where filter data in TableViewController according to the values. It is made with using UISwitch. Anyway, I would like to ask, how to add a SVProgressHUD to the action, when I return back from filterViewController with filtered Array. It takes some secs to filter and display them in tableView. I already tried dispatch_async and it doesn't even display HUD. I have an IBAction for button in filterViewController to confirm values and send them to TableViewController.
If I added HUD to the action in filterViewController, HUD doesn't display in TableViewController.
filterViewController.m
-(IBAction)Done:(id)sender{
[self willMoveToParentViewController:nil];
[self.navigationController popViewControllerAnimated:YES];
}
TableViewController.m
- (void)filterController:(filterViewController *)controller didEditConfig:(NSMutableDictionary *)config
{
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// time-consuming task
self.konfigg = config;
NSSet *filter = [config keysOfEntriesPassingTest:
^BOOL (id key, NSNumber *value, BOOL *stop) {
return [value boolValue];
}];
NSLog(#"filtered keys: %# (%lu of %lu)", filter, (unsigned long)filter.count, (unsigned long)config.count);
PFQuery *query = [PFQuery queryWithClassName:#"Class"];
[query addDescendingOrder:#"createdAt"];
[query whereKey:#"eventDay" containedIn:[filter allObjects]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.itemss = [objects mutableCopy];
[self.MainTable reloadData];
NSLog(#"Got filtered results %# (%lu)", objects, (unsigned long)objects.count);
}}
];
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
});
});
}
Got it. Made just BOOL for the filter in TableViewController. It was easier than I thought.

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!

Trouble accessing Mutable array

Im having trouble reassigning data in an array where I am trying to index user names. I am able to separate my original array into individual objects but am not able to send the value to a new array that I need to reference later on. The value and count for userNames in my self.userNamesArray = userNames; line is correct. But right after that when I log self.userNamesArray, I get (null). Any tips cause I'm not completely sure I'm cheers!
.h
#property (nonatomic, copy) NSMutableArray *userNamesArray;
.m
- (void)viewWillAppear:(BOOL)animated {
self.friendsRelation = [[PFUser currentUser] objectForKey:#"friendsRelation"];
PFQuery *query = [self.friendsRelation query];
[query orderByAscending:#"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
else {
self.friends = objects;
NSArray *users = [self.friends valueForKey:#"username"];
NSLog(#"username:%#", users);
//Create an array of name wrappers and pass to the root view controller.
NSMutableArray *userNames = [[NSMutableArray alloc] initWithCapacity:[self.friends count]];
for (NSString *user in users) {
componentsSeparatedByCharactersInSet:charSet];
NSArray *nameComponents = [user componentsSeparatedByString:#" "];
UserNameWrapper *userNameWrapper = [[UserNameWrapper alloc] initWithUserName:nil nameComponents:nameComponents];
[userNames addObject:userNameWrapper];
}
self.userNamesArray = userNames;
NSLog(#"userNamesArray:%#",self.userNamesArray);
[self.tableView reloadData];
}
Here's the code where I need to reference the self.userNamesArray where again, it is comping up nil.
- (void)setUserNamesArray:(NSMutableArray *)newDataArray {
if (newDataArray != self.userNamesArray) {
self.userNamesArray = [newDataArray mutableCopy];
if (self.userNamesArray == nil) {
self.sectionsArray = nil;
NSLog(#"user names empty");
}
else {
[self configureSections];
}
}
}
Change your property method of mutable array to below:-
#property (nonatomic, retain)
NSMutableArray *userNamesArray;
Is this code calling itself recursively?
self.userNamesArray = [newDataArray mutableCopy];
is equivilent to:
[self setUserNamesArray: [newDataArray mutableCopy]];
If you need to override what happens during assignment, you can do as you're doing here but use _userNamesArray to reference the underlying member field.
First of all, I don't you need NSMutableArray for "userNamesArray". You could simply use NSArray. Now, try with below piece of code and you should be good to go:
self.userNamesArray = [NSMutableArray arrayWithArray:userNames];
You might get null because of this line:
NSArray *users = [self.friends valueForKey:#"username"];
Change it to:
NSArray *users = [self.friends objectForKey:#"username"];
In addition, follow #Abhinav suggestion to have more cleaner code :)

Resources