Swift, Parse.Com: How to Pass Data from Query

Swift, parse.com: how to pass data from query

Instead of using findObjectsInBackgroundWithBlock which runs asynchronously, try using findObjects which runs synchronously:

//Set up query...
var objects = query.findObjects()
numObjects = objects.count
println(numObjects)

Then when running your function, do it like so:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) {
//Search users
searchUserInParse()

dispatch_async(dispatch_get_main_queue()) {
//Show number of objects etc.
}
}

How to pass data from query - Swift and Parse

Just call self.tableView.reloadData()

import UIKit
import Parse

class ListaTableViewController: UITableViewController {

var queryArray: [PFObject] = [PFObject]()

override func viewDidLoad() {
super.viewDidLoad()
var query = PFQuery(className:"Restaurantes")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in

if error == nil {
println("Successfully retrieved \(objects!.count) Restaurantes.")
if let _objects = objects as? [PFObject] {
self.queryArray = _objects
self.tableView.reloadData()
}
} else {
println("Error: \(error!) \(error!.userInfo!)")
}
}
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return queryArray.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> ListaTableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ListaTableViewCell
let restaurante = queryArray[indexPath.row] as! PFObject
cell.textCell.text = restaurante.objectForKey("nome") as! NSString
cell.imageBg.image = UIImage(named: "www.maisturismo.jpg")

return cell
}

How to pass data from parse table into table view in swift?

Great to hear you're getting started with app development!

This is a very common scenario and will be a breeze to accomplish with the right tools. You can build out your own functionality for Parse within a TableViewController or you can use Parse's own PFQueryTableViewController to accomplish exactly what you want very easily.

Check out a tutorial to help get you started

Here's another just in case

The gist of it is, you must query Parse for data to fill the TableViewController's data source. With PFQueryTableViewController, all you have to do is specify a query in the function queryForTable(). PFQueryTableViewController is included in the ParseUI framework (along with a bunch of other great tools) which you will want to import.

How do I pass variables to Cloud Code from Swift?

Because you are sending B08873 as key and This is a test message. as its value. If you want to send both channel and message key/value pairs you need to do it like this instead:

PFCloud.callFunctionInBackground("alertUser", withParameters: ["channels": channels , "message": message], .....

In your Cloud function you should be bale to access these paramerts like this:

var theChannels  = request.params.channels;  // returns B08873
var theMessage = request.params.message; // returns This is a test message.

Then call the Push function like this:

Parse.Push.send({
channels: [ theChannels ],
data: {
alert: theMessage ,
badge: "Increment"
}}, {
success: function() {
response.success();
},
error: function(error) {
response.error(error);
}
});

Pass data between two Apps by url scheme in swift?

Yes, you can use query string.

url.query contains the query string. For example, in the URL
iOSTest://www.example.com/screen1?textSent="Hello World", the query string is textSent="Hello World".

Normally we use URLSchemes for deeplinking too, therefore the URLScheme specifies which app to open and the path in the url specifies which screen to open and query string has additional parameters which we want to send to the app.

url.query is a string, therefore you will have to parse it to get the value you need:
For example, in the URL iOSTest://www.example.com/screen1?key1=value1&key2=value2, the query string is key1=value1&key2=value2. I'm writing code to parse it but make sure you test it for your case:

    let params = NSMutableDictionary()
let kvPairs : [String] = (url.query?.componentsSeparatedByString("&"))!
for param in kvPairs{
let keyValuePair : Array = param.componentsSeparatedByString("=")
if keyValuePair.count == 2{
params.setObject(keyValuePair.last!, forKey: keyValuePair.first!)
}
}

params will contain all key value pairs in query string.
Hope it helps :]

In case you don't want to do deep-linking, you can directly append the queryString to scheme. eg: iOSTest://?textSent="Hello World"

Best way to parse URL string to get values for keys?

edit (June 2018): this answer is better. Apple added NSURLComponents in iOS 7.

I would create a dictionary, get an array of the key/value pairs with

NSMutableDictionary *queryStringDictionary = [[NSMutableDictionary alloc] init];
NSArray *urlComponents = [urlString componentsSeparatedByString:@"&"];

Then populate the dictionary :

for (NSString *keyValuePair in urlComponents)
{
NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="];
NSString *key = [[pairComponents firstObject] stringByRemovingPercentEncoding];
NSString *value = [[pairComponents lastObject] stringByRemovingPercentEncoding];

[queryStringDictionary setObject:value forKey:key];
}

You can then query with

[queryStringDictionary objectForKey:@"ad_eurl"];

This is untested, and you should probably do some more error tests.

Swift & Parse: Query Results Where Column Class Is Equal To A Column In Users Class

let userQuery = PFUser.query();
// Set up user query
let issueQuery = PFQuery(className:"Issue");
issueQuery.whereKey("WorkGroup", matchesKey:"WorkGroup", inQuery:userQuery);

http://parseplatform.org/Parse-SDK-iOS-OSX/api/Classes/PFQuery.html#/c:objc(cs)PFQuery(im)whereKey:matchesKey:inQuery:

How to check if background queries have been completed in Parse.com with Swift

Use dispatch groups. Check here in the "Waiting on Groups of Queued Tasks" for more information.

Also you mentioned you want to get all objects of every user. So instead of getFirstObject you can use findObjectsInBackgroundWithBlock method. The block returns the parsed response. So combining this and the dispatch groups your code can look something like this

//create a new dispatch group
let group: dispatch_group_t = dispatch_group_create();
var myObjects = []
for user in users { // users is an array of PFUsers
let query = PFQuery(className: "MyClass")
query.whereKey("asdh74tbf", equalTo: user)

//enter the dispatch group
dispatch_group_enter(group);

query.findObjectsInBackgroundWithBlock{ (objects: NSArray!, error: NSError!) -> Void in
if let object = object {
self.myObjects.append(object)
} else {
// handle error
}

//leave the dispatch group
dispatch_group_leave(group);
}
}

//will be called when all the async operations entered in the group are finished.
dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
self.tableView.reloadData()
});


Related Topics



Leave a reply



Submit