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.
Related
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");
}
}];
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];.
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]
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]);
}
}];
}
I am showing an several images using an UIScrollView. however I would want it to refresh automatically.
I want to refresh it every time I show the view controller.
Using my code I have to push the refresh button every time which is just nonsense.
Here is the code : How can i do this ?
- (IBAction)refresh:(id)sender
{
NSLog(#"Showing Refresh HUD");
refreshHUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:refreshHUD];
// Register for HUD callbacks so we can remove it from the window at the right time
refreshHUD.delegate = self;
// Show the HUD while the provided method executes in a new thread
[refreshHUD show:YES];
PFQuery *query = [PFQuery queryWithClassName:#"TPhoto"];
PFUser *user = [PFUser currentUser];
[query whereKey:#"user" equalTo:user];
[query orderByAscending:#"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
if (refreshHUD) {
[refreshHUD hide:YES];
refreshHUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:refreshHUD];
// The sample image is based on the work by http://www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
// Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
refreshHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"37x-Checkmark.png"]];
// Set custom view mode
refreshHUD.mode = MBProgressHUDModeCustomView;
refreshHUD.delegate = self;
}
NSLog(#"Successfully retrieved %d photos.", objects.count);
// Retrieve existing objectIDs
NSMutableArray *oldCompareObjectIDArray = [NSMutableArray array];
for (UIView *view in [photoScrollView subviews]) {
if ([view isKindOfClass:[UIButton class]]) {
UIButton *eachButton = (UIButton *)view;
[oldCompareObjectIDArray addObject:[eachButton titleForState:UIControlStateReserved]];
}
}
NSMutableArray *oldCompareObjectIDArray2 = [NSMutableArray arrayWithArray:oldCompareObjectIDArray];
// If there are photos, we start extracting the data
// Save a list of object IDs while extracting this data
NSMutableArray *newObjectIDArray = [NSMutableArray array];
if (objects.count > 0) {
for (PFObject *eachObject in objects) {
[newObjectIDArray addObject:[eachObject objectId]];
}
}
// Compare the old and new object IDs
NSMutableArray *newCompareObjectIDArray = [NSMutableArray arrayWithArray:newObjectIDArray];
NSMutableArray *newCompareObjectIDArray2 = [NSMutableArray arrayWithArray:newObjectIDArray];
if (oldCompareObjectIDArray.count > 0) {
// New objects
[newCompareObjectIDArray removeObjectsInArray:oldCompareObjectIDArray];
// Remove old objects if you delete them using the web browser
[oldCompareObjectIDArray removeObjectsInArray:newCompareObjectIDArray2];
if (oldCompareObjectIDArray.count > 0) {
// Check the position in the objectIDArray and remove
NSMutableArray *listOfToRemove = [[NSMutableArray alloc] init];
for (NSString *objectID in oldCompareObjectIDArray){
int i = 0;
for (NSString *oldObjectID in oldCompareObjectIDArray2){
if ([objectID isEqualToString:oldObjectID]) {
// Make list of all that you want to remove and remove at the end
[listOfToRemove addObject:[NSNumber numberWithInt:i]];
}
i++;
}
}
// Remove from the back
NSSortDescriptor *highestToLowest = [NSSortDescriptor sortDescriptorWithKey:#"self" ascending:NO];
[listOfToRemove sortUsingDescriptors:[NSArray arrayWithObject:highestToLowest]];
for (NSNumber *index in listOfToRemove){
[allImages removeObjectAtIndex:[index intValue]];
}
}
}
// Add new objects
for (NSString *objectID in newCompareObjectIDArray){
for (PFObject *eachObject in objects){
if ([[eachObject objectId] isEqualToString:objectID]) {
NSMutableArray *selectedPhotoArray = [[NSMutableArray alloc] init];
[selectedPhotoArray addObject:eachObject];
if (selectedPhotoArray.count > 0) {
[allImages addObjectsFromArray:selectedPhotoArray];
}
}
}
}
// Remove and add from objects before this
[self setUpImages:allImages];
} else {
[refreshHUD hide:YES];
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
Call the refresh method from viewDidAppear in your view controller.
However, if that can happen often, you should consider keeping the images in a local cache and only refresh them if there is something new to show on the server.