When I try to store arrays from Parse into a local array I can only access it within the findObjectsInBackgroundWithBlock {...}. When I print it outside of that block, it shows []...
Code :
var qArray : [[Int]] = []
override func viewDidLoad() {
super.viewDidLoad()
let query = PFQuery(className: "Trivia")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) in
if objects != nil {
if let objects = objects {
for object in objects {
self.qArray.append(object["mainPattern"] as! [Int])
}
print(self.qArray) // Prints a multi dimension array
}
}
if error != nil {
print(error)
}
}
print(self.qArray) // prints []
}
It's most likely because the array hasn't been populated yet because it's running in the background. You can try using dispatch_group to circumvent this issue.
I think you're misunderstanding what findInBackground means. It means the code after the callback continues to execute, so it calls query.findInBackground.... and then it continues with the next line, which is print(self.qArray). At some point later on, it hears back from the database and it executes all the code inside the Callback, which is when the array finally gets populated.
Related
I have an empty global array. The only simple thing I want to do is add an element to this array. It seems in swift this seemingly simple task is proving to be difficult. I am just left with an empty array and nothing is appending to my global array.
I can see that it prints out values in the for loop. So the values are actually there.
This is some stuff I have declared globally (Yes, I know global variables are bad but I will sort that out later):
struct HouseDetails: Decodable {
let median_price: String
let sale_year: String
let transaction_count: String
let type: String
}
var hsArray: [HouseDetails] = []
and in the viewDidLoad() function I have the data which I am storing in local variable "houses". When I loop through the array it prints median_price, showing that the values are there.
However when I do hsArray.append(h) it seems to do nothing.
let jsonUrlString = "https://data.melbourne.vic.gov.au/resource/i8px-csib.json"
guard let url = URL(string: jsonUrlString)
else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else { return }
do {
let houses = try JSONDecoder().decode([HouseDetails].self, from: data)
for h in houses {
hsArray.append(h)
print(h.median_price)
}
}
catch let jsonErr {
print("Error with json serialization", jsonErr)
}
}.resume()
Thank you for any help. In other languages I am used to being able to append an element to the end of an existing array, so I am sure it is just a small error.
Firstly, why don't you simply do
hsArray.append(contentsOf: houses)
instead of all that for loop
for h in houses {
hsArray.append(h)
print(h.median_price)
}
The issue might be the time at which you are using hsArray. See if the response is received after you use hsArray.
I have this closure which I use to populate my array and dictionary. However, when I try to use it outside the function, it's empty. Here's my code:
var pages: [FBPages] = []
override func viewDidLoad() {
super.viewDidLoad()
Facebook.getUserInfo(greetingLabel: greetingLabel, profileImage: profilePic)
pages = []
let params = ["fields": "about,name,created_time,picture", "limit": "3"]
Facebook.getUserPagesLikes(params: params, handler: { (userData) in
guard let pagesArrays = userData["data"] as? Array<Any> else {return}
for dict in pagesArrays {
let fbPages = FBPages()
let pagesData = dict as! NSDictionary
fbPages.about = pagesData["about"] as! String
fbPages.name = pagesData["name"] as! String
self.pages.append(fbPages)
}
print("pages array: \(pagesArrays)")
print("pages : \(self.pages.count)")
}) { (error) in
print("cannot get FB pages: \(String(describing: error))")
}
//I got 0 here
print("fb array: \(pages.count)")
}
I tried to print array count outside closure but I only got 0 as a result. How can I solve this problem. Thanks in advance.
You will need to understand that the time you are printing array. It is not yet available. getUserPagesLikes is an async call. Which is taking a completion handler. Once the response comes back, you will have values in your array.
You can understand the sequence like you ask getUserPagesLikes to fetch the user likes for you and you don't wait for user likes there. You move ahead, and whatever code you write after completion block gets called. Which in your case turns out to be printing array.
Once the getUserPagesLikes gets back the data for you its completion block gets called. At this time you assign the array values. The values in array would be available after this only. So if you want to reload data in table view do it in completion handler. Also make sure that you do it on main thread.
For a detail information on completion handler and their sequence of call you can go through here.
Im new to Swift and Im having a hard time understanding why this is not working - I've tried many different combinations of this through examples on stackoverflow and my variable "collections" still comes out empty (see last line) so I'm guessing I'm missing a small (but important) detail. Appreciate any help!
class CollectionsViewController: UITableViewController {
var collections = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
SDK.sharedInstance()
.getAuthenticatedUserBoards(withFields: ["id", "name","url","description","image"],
success: { data in
guard let myData = data?.parsedJSONDictionary["data"] as? [[String: Any]]
else {
return
}
for item in myData {
self.collections.add(item)
}
}, andFailure: nil)
print("collections.....\(collections)")
//Output: collections.....()
}
The loading of the data is asynchronous. Since your goal to load your table view after the data loads, you need to call reloadData on your table view at the end of the success block. But UI calls must be made on the main queue so you should use DispatchQueue to do this.
Here is what you need:
override func viewDidLoad() {
super.viewDidLoad()
SDK.sharedInstance()
.getAuthenticatedUserBoards(withFields: ["id", "name","url","description","image"],
success: { data in
guard let myData = data?.parsedJSONDictionary["data"] as? [[String: Any]]
else {
return
}
for item in myData {
self.collections.add(item)
}
print("collections.....\(collections)")
DispatchQueue.main.async {
tableView.reloadData()
}
}, andFailure: nil)
}
Since you are currently updating self.collections in the background, there is a small chance your UI on the main queue will see a partially up-to-date set of data. So I would recommend one further change:
override func viewDidLoad() {
super.viewDidLoad()
SDK.sharedInstance()
.getAuthenticatedUserBoards(withFields: ["id", "name","url","description","image"],
success: { data in
guard let myData = data?.parsedJSONDictionary["data"] as? [[String: Any]]
else {
return
}
var list = NSMutableArray()
for item in myData {
list.add(item)
}
DispatchQueue.main.async {
self.collections = list
print("collections.....\(collections)")
tableView.reloadData()
}
}, andFailure: nil)
}
This ensure the main collections property is only updated on the main thread.
I also suggest you use a Swift array of a specific type instead of using an NSMutableArray.
I'm assuming getAuthenticatedUserBoards is making some sort of asynchronous call to an API. Since we're not sure how long that call is going to take, the task is placed on a background thread while the rest of the app's tasks continue to run on the main thread. Your print statement is one of those tasks that will run while the API call is happening in the background. In essence, the print statement executes before anything inside of the getAuthenticatedUserBoards is completed.
The list of collections should be printed directly after the for loop and before the end of the closure. You can verify the order in which the different tasks are completed by placing print statements at various different points within your function - you should be able to see that your current print statement will print before any other statements that you place inside of the closure.
I am trying to populate an array with a query from my parse database. When I try to print out the content of the array, I get an EXC_BAD_INSTRUCTION error. It doesn't seem like I'm properly appending new elements into my array, I'd appreciate any sort of tips
func loadSampleTasks() {
tasks = [Task]()
let query = PFQuery(className: "Task")
query.whereKey("TaskName", equalTo: "kenny")
query.findObjectsInBackgroundWithBlock() {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil && objects != nil {
self.parseResults(objects!)
print(self.tasks) // this prints out Kenny object as expected
}
}
print(tasks) // prints an empty array
}
func parseResults(objects: Array<PFObject>){
for object in objects { //looping through returned data
print("no error in Parse lookup")
let parseResult1 = Task(name: object["TaskName"] as! String)
parseResult1?.completed = object["Completed"] as! Bool
print("Parse result in object loop: \(parseResult1!.name)")
tasks.append(parseResult1!)
}
}
Any help much appreciated!
To what thefredelement said, "This is happening because you're getting your parse results in a closure, which means it may execute after the function itself has already returned." Before this happens, though, it won't work properly and you'll come back with that error.
I got it to work. I had to tableView.reloadData() within the closure. Thank you so much for the help!
I want the user to upload an image, and other users leave replies to that image. Everything works fine so far except if the row for that specific image object is empty, the app crashes.
fatal error: unexpectedly found nil while unwrapping an Optional value
Here is my code :
override func viewDidLoad() {
super.viewDidLoad()
var error = ""
var query = PFQuery(className:"Posts")
query.getObjectInBackgroundWithId(cellID) {
(objects: PFObject!, error: NSError!) -> Void in
if error == nil {
var array = objects.objectForKey("replies") as [String] // <- when error occurs the compiler point here.
for object in array {
self.repliesArray.append(object as String)
}
} else {
self.displayError("Error", error: "Error retreiving")
}
self.tableView.reloadData()
}
}
Does this work? You can't append null objects to an array so this appends an empty string instead of null. Also you need the explanation mark so that it can be nil.
var array = objects.objectForKey("replies") as [String!]
for object in array {
if object != nil {
self.repliesArray.append(object as String)
}
else {
self.repliesArray.append("")
}
}
query.getObjectInBackgroundWithId(cellID) {
(objects: PFObject!, error: NSError!) -> Void in
if error == nil {
var array = objects.objectForKey("replies") as [String] // <- when error occurs the compiler point here.
The key here is that you're accepting an implicitly unwrapped optional (PFObject!) which is implying a promise that it will never be nil, but is nil.
Ideally (*), the type should be (objects: PFObject?, error: NSError?) -> Void to make the optionals explicit and force you to do the nil checking that is required. I believe you can do this even if the caller claims to send you (PFObject!, NSError!) -> Void, since I think Swift will make the implicit->explicit conversion for you.
If that's still impossible, then you will have to manually verify that objects is not nil before using it. (This is unlike #Dehli's solution, which checks that the things contained in objects are non-nil. That's not the problem; the problem is that objects itself is nil.)
(*) I say "ideally" here in terms of the existing interface. Passing a tuple of optional value/error is a lousy pattern. The better solution is to use something like a Result object.