Brenden Belluardo Brenden Belluardo - 1 year ago 89
Swift Question

objects not being added to an array - swift iOS

I'm trying to add Task objects to an array called tasks, and then filtered them into another array called filteredTasks by category.

var tasks = [Task]()
var filteredTasks = [Task]()

override func viewDidLoad() {
if filteredTasks.isEmpty {
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.delegate = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self

// Do any additional setup after loading the view, typically from a nib.

func loadTasks() {
var taskTitle: String?
var id: String?
var distance: Double?
var category: String?

var locationDict: [String:CLLocation] = [:]
var refHandle = self.ref.child("tasks").observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
self.tasksDict = snapshot.value as? NSDictionary
for i in 0 ..< self.tasksDict!.count {
let taskId = self.tasksDict!.allKeys[i] as! String
print ("1")
id = taskId
print (taskId)
let task = self.tasksDict!.objectForKey(taskId) as! NSDictionary
print ("2")
let lat = task.objectForKey("latitude") as! String
let long = task.objectForKey("longitude") as! String
let latNum = Double(lat)! as CLLocationDegrees
let longNum = Double(long)! as CLLocationDegrees
taskTitle = task.objectForKey("title") as? String
category = task.objectForKey("category") as? String
// print (taskTitle)
//print (category)

let pointLocation = CLLocation(latitude: latNum, longitude: longNum)
locationDict[taskId] = pointLocation
if (self.locationCurrent == nil) {
self.locationCurrent = self.locationManager.location!
distance = round(self.locationCurrent!.distanceFromLocation(pointLocation))
var taskAddition = Task(title: taskTitle, id: id, distance: distance, category: category)
print (taskAddition)
self.tasks += [taskAddition]


func filterTasks(searchText: String, scope: String = "All") {
for i in 0 ..< tasks.count {
let taskId = tasksDict!.allKeys[i] as! String
// print (taskId)
let task = tasksDict!.objectForKey(taskId) as! NSDictionary
let taskCategory = task.objectForKey("category") as! String
if (taskCategory.lowercaseString.containsString(searchText.lowercaseString)) {
filteredTasks.append(task[i] as! Task)



The numbers 1,2,3, and 4 all print out in the console, but if I try to print out tasks or filteredTasks, I get [] and a count of 0. Is there a reason why tasks aren't being added to the arrays?

Answer Source

You can't do it that way. The method observeEventType is asynchronous. It takes a closure as a parameter. It returns immediately, before the closure is run.

You need to rewrite your loadTasks function to also take a completion closure. You'd then call it with the code that you want to execute once the tasks array is filled. See my answer to this thread for a working example:

how to return value after the execution of the block? Swift

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download