Remove complete element from Array Swift - ios

Im reading documents from firebase and putting them into an Array. The output of my array is:
[MainApp.Message(name: Optional("Bluesona"), userId:
Optional("7epbTeafCAS51ctDxWWt0xIAWN03"), msg: Optional(""), creatAt:
Optional(1535028200082), latitude: Optional("0.00"), longitude:
Optional("0.00")), MainApp.Message(name: Optional("Oliver"), userId:
Optional("7epbTeafCAS51ctDxWWt0xIAWN03"), msg: Optional(""), creatAt:
Optional(1537440120260), latitude: Optional("54.976663"), longitude:
Optional("-7.732037")), MainApp.Message(name: Optional("Oliver"),
userId: Optional("7epbTeafCAS51ctDxWWt0xIAWN03"), msg: Optional(""),
creatAt: Optional(1537639139566), latitude: Optional("54.976726"),
longitude: Optional("-7.731986"))]
How can I then manipulate my array to only display items which have name "Bluesona"?
Here is the code where I'm creating my array
firebaseDB.collection("message").document(key).collection("messages").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
}
else {
self.dataArr.removeAll()
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
let msgdata = document.data() as! [String:Any]
var msgObj = Message()
if let name = msgdata["name"] as? String {
msgObj.name = name
}
if let latitude = msgdata["latitude"] as? String {
msgObj.latitude = latitude
}
if let long = msgdata["longitude"] as? String {
msgObj.longitude = long
}
if let uid = msgdata["userId"] as? String {
msgObj.userId = uid
}
if let time = msgdata["createdAt"] as? Int {
msgObj.creatAt = time
}
self.dataArr.append(msgObj)
// self.dataArr.append(document.data() as [String:Any])
}
self.dataArr = self.dataArr.sorted(by: {$0.creatAt < $1.creatAt })
self.tblMessage.reloadData()
if self.dataArr.count < 1 {
print("No Messages")
}
else{
//self.tblMessage.scrollToRow(at: IndexPath(row: self.dataArr.count - 1, section: 0), at: .bottom, animated: true)
print(self.dataArr)
}
}
}

You can use filter to achieve your goals.
...
self.dataArr = self.dataArr.sorted(by: {$0.creatAt < $1.creatAt })
self.dataArr = dataArr.filter { $0.name == "Bluesona"}
self.tblMessage.reloadData()
...
Or you can also use removeAll(where:) introduced in Swift4.2:
...
self.dataArr = self.dataArr.sorted(by: {$0.creatAt < $1.creatAt })
self.dataArr.removeAll(where: { $0.name != "Bluesona"})
self.tblMessage.reloadData()
...

Related

How can I guarantee that a Swift closure (referencing Firebase) fully executes before I move on?

I have multiple query snapshots with closures and some of them are using the data supplied by the query that came before it.
I have read up on GCD and I've tried to implement a DispatchGroup with .enter() and .leave() but I am apparently doing something wrong.
If somebody can help me by laying out exactly how to force one task to be performed before another, that would solve my problem.
If you can't tell, I am somewhat new to this so any help is greatly appreciated.
//MARK: Get all userActivities with distance(All Code)
static func getAllChallengesWithDistanceAllCode(activity:String, completion: #escaping ([Challenge]) -> Void) {
let db = Firestore.firestore()
let currUserID = Auth.auth().currentUser!.uid
var currUserName:String?
var distanceSetting:Int?
var senderAverage:Double?
var senderBestScore:Int?
var senderMatchesPlayed:Double?
var senderMatchesWon:Double?
var senderWinPercentage:Double?
var validUserActivities = [Challenge]()
db.collection("users").document(currUserID).getDocument { (snapshot, error) in
if error != nil || snapshot == nil {
return
}
currUserName = snapshot?.data()!["userName"] as? String
distanceSetting = snapshot?.data()!["distanceSetting"] as? Int
}
db.collection("userActivities").document(String(currUserID + activity)).getDocument { (snapshot, error) in
//check for error
//MARK: changed snapshot to shapshot!.data() below (possible debug tip)
if error != nil {
//is error or no data..??
return
}
if snapshot!.data() == nil {
return
}
//get profile from data proprty of snapshot
if let uActivity = snapshot!.data() {
senderBestScore = uActivity["bestScore"] as? Int
senderMatchesPlayed = uActivity["played"] as? Double
senderMatchesWon = uActivity["wins"] as? Double
senderAverage = uActivity["averageScore"] as? Double
senderWinPercentage = round((senderMatchesWon! / senderMatchesPlayed!) * 1000) / 10
}
}
if distanceSetting != nil {
db.collection("userActivities").whereField("activity", isEqualTo: activity).getDocuments { (snapshot, error) in
if error != nil {
print("something went wrong... \(String(describing: error?.localizedDescription))")
return
}
if snapshot == nil || snapshot?.documents.count == 0 {
print("something went wrong... \(String(describing: error?.localizedDescription))")
return
}
if snapshot != nil && error == nil {
let uActivitiesData = snapshot!.documents
for uActivity in uActivitiesData {
let userID = uActivity["userID"] as! String
UserService.determineDistance(otherUserID: userID) { (determinedDistance) in
if determinedDistance! <= distanceSetting! && userID != currUserID {
var x = Challenge()
//Sender
x.senderUserID = currUserID
x.senderUserName = currUserName
x.senderAverage = senderAverage
x.senderBestScore = senderBestScore
x.senderMatchesPlayed = senderMatchesPlayed
x.senderMatchesWon = senderMatchesWon
x.senderWinPercentage = senderWinPercentage
//Receiver
x.receiverUserID = userID
x.receiverUserName = uActivity["userName"] as? String
x.receiverAverage = uActivity["averageScore"] as? Double
x.receiverBestScore = uActivity["bestScore"] as? Int
if (uActivity["played"] as! Double) < 1 || (uActivity["played"] as? Double) == nil {
x.receiverMatchesPlayed = 0
x.receiverMatchesWon = 0
x.receiverWinPercentage = 0
} else {
x.receiverMatchesPlayed = uActivity["played"] as? Double
x.receiverMatchesWon = uActivity["wins"] as? Double
x.receiverWinPercentage = ((uActivity["wins"] as! Double) / (uActivity["played"] as! Double) * 1000).rounded() / 10
}
if uActivity["playStyle"] as? String == nil {
x.receiverPlayStyle = "~No PlayStyle~"
} else {
x.receiverPlayStyle = uActivity["playStyle"] as! String
}
x.activity = activity
//append to array
validUserActivities.append(x)
}
}
}
completion(validUserActivities)
}
}
}
}
try this
static func getAllChallengesWithDistanceAllCode(activity:String, completion: #escaping ([Challenge]) -> Void) {
let db = Firestore.firestore()
let currUserID = Auth.auth().currentUser!.uid
var currUserName:String?
var distanceSetting:Int?
var senderAverage:Double?
var senderBestScore:Int?
var senderMatchesPlayed:Double?
var senderMatchesWon:Double?
var senderWinPercentage:Double?
var validUserActivities = [Challenge]()
db.collection("users").document(currUserID).getDocument { (snapshot, error) in
if error != nil || snapshot == nil {
return
}
currUserName = snapshot?.data()!["userName"] as? String
distanceSetting = snapshot?.data()!["distanceSetting"] as? Int
db.collection("userActivities").document(String(currUserID + activity)).getDocument { (snapshot, error) in
//check for error
//MARK: changed snapshot to shapshot!.data() below (possible debug tip)
if error != nil {
//is error or no data..??
return
}
if snapshot!.data() == nil {
return
}
//get profile from data proprty of snapshot
if let uActivity = snapshot!.data() {
senderBestScore = uActivity["bestScore"] as? Int
senderMatchesPlayed = uActivity["played"] as? Double
senderMatchesWon = uActivity["wins"] as? Double
senderAverage = uActivity["averageScore"] as? Double
senderWinPercentage = round((senderMatchesWon! / senderMatchesPlayed!) * 1000) / 10
if snapshot != nil && error == nil {
let uActivitiesData = snapshot!.documents
for uActivity in uActivitiesData {
let userID = uActivity["userID"] as! String
UserService.determineDistance(otherUserID: userID) { (determinedDistance) in
if determinedDistance! <= distanceSetting! && userID != currUserID {
var x = Challenge()
//Sender
x.senderUserID = currUserID
x.senderUserName = currUserName
x.senderAverage = senderAverage
x.senderBestScore = senderBestScore
x.senderMatchesPlayed = senderMatchesPlayed
x.senderMatchesWon = senderMatchesWon
x.senderWinPercentage = senderWinPercentage
//Receiver
x.receiverUserID = userID
x.receiverUserName = uActivity["userName"] as? String
x.receiverAverage = uActivity["averageScore"] as? Double
x.receiverBestScore = uActivity["bestScore"] as? Int
if (uActivity["played"] as! Double) < 1 || (uActivity["played"] as? Double) == nil {
x.receiverMatchesPlayed = 0
x.receiverMatchesWon = 0
x.receiverWinPercentage = 0
} else {
x.receiverMatchesPlayed = uActivity["played"] as? Double
x.receiverMatchesWon = uActivity["wins"] as? Double
x.receiverWinPercentage = ((uActivity["wins"] as! Double) / (uActivity["played"] as! Double) * 1000).rounded() / 10
}
if uActivity["playStyle"] as? String == nil {
x.receiverPlayStyle = "~No PlayStyle~"
} else {
x.receiverPlayStyle = uActivity["playStyle"] as! String
}
x.activity = activity
//append to array
validUserActivities.append(x)
}
}
}
completion(validUserActivities)
}
}
}
}
}
}
}
I solved this by doing the following:
Add a constant before the for-in loop counting the total number of query results
let documentCount = snapshot?.documents.count
Add a counter before the for-in loop starting at 0
var runCounter = 0
Increment the counter with each iteration at the beginning of the for-in loop
runCounter += 1
Add code to the end of the for-in loop to call the completion handler to return the results
if runCounter == documentCount {
completion(validChallenges)
}

How to fix memory issues given by Instrument tools in Swift?

I have memory issues, especially for the error
XPC connection interrupted
The screen is freezing for a few seconds..
So, I've been learning how to use the Instruments tools and try to fix this error. However, I've been trying to find the error in my code and it's apparently not the fault of my code but maybe the libraries?
As a result of this test, I've got some warnings (the purple ones):
Memory Issues (3 leaked types):
- 1 instance of _DateStorage leaked (0x10b1eb060)
- 1 instance of OS_dispatch_data leaked (0x10b0b1ac0)
- 1 32-byte malloc block leaked (x10b1eb040)
Could you tell me how to fix these warnings, knowing there is no backtrace available? Or how could I find somewhere that could tell me to fix those?
EDIT:
Thanks to Instrument tools, I found the function that caused the problem! So, I don't know if it is really about memory or Idk but here's the function!
The accurate and useful error I get is : "Closure #1 in closure #1 in MessagesTableViewController.getLastMessages"
I found here What is a closure #1 error in swift?, the error is probably caused by forced optional types. So, I am going to try to remove those.
func getLastMessages(cell: ContactMessageTableViewCell, index: IndexPath) {
// first, we get the total number of messages in chatRoom
var numberOfMessagesInChatRoom = 0
let previousCellArray = self.tableView.visibleCells as! [ContactMessageTableViewCell]
var index1 = 0
var messages = [JSQMessage]()
var sortedMessages = [JSQMessage]()
var messagesSortedByChatRooms = [String: [JSQMessage]]()
var doesHaveMessagesCount = false
var doesHaveSortedMessagesCount = false
let firstQuery = Constants.refs.databaseChats.queryOrderedByKey()
_ = firstQuery.observe(.childAdded, with: { [weak self] snapshot in
if let data = snapshot.value as? [String: String],
let id = data["sender_id"],
let name = data["name"],
let text = data["text"],
let chatRoom = data["chatRoom"],
!text.isEmpty
{
if let message = JSQMessage(senderId: id, displayName: name, text: text)
{
messages.append(message)
var arrayVariable = [JSQMessage]()
// we wanna get all messages and put it in the array corresponding to the chat room key
if messagesSortedByChatRooms[chatRoom] != nil { // if there is already the chatRoom key in dictionary
if let message1 = messagesSortedByChatRooms[chatRoom] {
arrayVariable = message1
}
arrayVariable.append(message)
messagesSortedByChatRooms[chatRoom] = arrayVariable
} else { // if there isn't the chatRoom key
arrayVariable.append(message)
messagesSortedByChatRooms[chatRoom] = arrayVariable
}
}
}
DispatchQueue.main.async {
// we have to sort messages by date
for (chatRoom, messagesArray) in messagesSortedByChatRooms {
var loopIndex = 0
var lastMessage: JSQMessage?
var array = [JSQMessage]()
for message in messagesArray { // we run through the messages array
array.removeAll()
loopIndex += 1
if loopIndex != 1 {
if message.date > lastMessage!.date {
array.append(message)
messagesSortedByChatRooms[chatRoom] = array
} else {
array.append(lastMessage!)
messagesSortedByChatRooms[chatRoom] = array
}
} else {
lastMessage = message
if messagesArray.count == 1 {
array.append(message)
messagesSortedByChatRooms[chatRoom] = array
}
}
}
}
if !doesHaveMessagesCount {
//doesHaveMessagesCount = true
// we have the number of chats in database
let secondQuery = Constants.refs.databaseChats.queryOrderedByPriority()
_ = secondQuery.observe(.childAdded, with: { [ weak self] snapshot in
if let data = snapshot.value as? [String: String],
let id = data["sender_id"],
let name = data["name"],
let text = data["text"],
let chatRoom = data["chatRoom"],
!text.isEmpty
{
if let message = JSQMessage(senderId: id, displayName: name, text: text)
{
index1 += 1
if chatRoom != nil {
if let unwrappedSelf = self {
if unwrappedSelf.sortedChatRoomsArray.contains(chatRoom) {
sortedMessages.append(message)
for (chatRoomKey, messageArray) in messagesSortedByChatRooms {
unwrappedSelf.lastMessages[chatRoomKey] = messageArray[0]
}
}
}
}
if let unwrappedSelf = self {
if index1 == messages.count && chatRoom != unwrappedSelf.roomName {
sortedMessages.append(JSQMessage(senderId: id, displayName: name, text: "no message"))
}
}
}
}
DispatchQueue.main.async {
if let unwrappedSelf = self {
if !doesHaveSortedMessagesCount {
//doesHaveSortedMessagesCount = true
if unwrappedSelf.sortedChatRoomsArray.indices.contains(index.row) {
if unwrappedSelf.lastMessages[unwrappedSelf.sortedChatRoomsArray[index.row]] != nil {
if unwrappedSelf.lastMessagesArray.count != 0 {
let currentChatRoom = unwrappedSelf.sortedChatRoomsArray[index.row]
if unwrappedSelf.lastMessages[unwrappedSelf.sortedChatRoomsArray[index.row]]?.text != "no message" {
if UUID(uuidString: unwrappedSelf.lastMessages[unwrappedSelf.sortedChatRoomsArray[index.row]]!.text) == nil {
cell.contactLastMessageLabel.text = unwrappedSelf.lastMessages[unwrappedSelf.sortedChatRoomsArray[index.row]]?.text
} else {
cell.contactLastMessageLabel.text = "New image"
}
} else {
cell.contactLastMessageLabel.text = ""
cell.contactLastMessageLabel.font = UIFont(name:"HelveticaNeue-Light", size: 16.0)
}
if unwrappedSelf.lastMessagesArray.indices.contains(index.row) {
if unwrappedSelf.lastMessagesArray[index.row] != unwrappedSelf.lastMessages[unwrappedSelf.sortedChatRoomsArray[index.row]]?.text {
if unwrappedSelf.lastMessages[unwrappedSelf.sortedChatRoomsArray[index.row]]?.senderId != PFUser.current()?.objectId {
if unwrappedSelf.lastMessages[unwrappedSelf.sortedChatRoomsArray[index.row]]?.text != "no message" {
cell.contactLastMessageLabel.font = UIFont(name:"HelveticaNeue-Bold", size: 16.0)
}
var numberOfDuplicates = 0
for cell in previousCellArray {
if cell.contactLastMessageLabel.text == unwrappedSelf.lastMessages[unwrappedSelf.sortedChatRoomsArray[index.row]]?.text {
numberOfDuplicates += 1
}
}
if numberOfDuplicates == 0 {
if unwrappedSelf.selectedUserObjectId != "" {
unwrappedSelf.changeCellOrder(index: index.row, selectedUserObjectId: unwrappedSelf.selectedUserObjectId, lastMessage: unwrappedSelf.lastMessages[unwrappedSelf.sortedChatRoomsArray[index.row]]!.text)
} else {
unwrappedSelf.changeCellOrder(index: index.row, selectedUserObjectId: "none", lastMessage: unwrappedSelf.lastMessages[unwrappedSelf.sortedChatRoomsArray[index.row]]!.text)
}
} else {
unwrappedSelf.tableView.reloadData()
}
cell.activityIndicatorView.stopAnimating()
}
} else {
cell.contactLastMessageLabel.font = UIFont(name:"HelveticaNeue-Light", size: 16.0)
}
}
}
} else {
}
}
}
}
}
})
}
}
})
}
FINL EDIT: I put a closure inside of another closure so it created an infine loop ;)

Check Jsonobject for Int

I have a problem, I get a Json. The data comes in a dictionary.
This is a sample json:
Receivedtext: {
"x": "pricef",
"b": "usd",
"ds": [
"tpr",
"avgp",
"mcap",
"ppc7D",
"ppc12h",
"ppc4h",
"ppc24h"
],
"data": [
[
"ADA/USD",
"0.819",
"21.23B",
"6.09",
"-5.45",
"-5.36",
"-10"
],
[
"AVT/USD",
"5.968",
"35.81M",
"24.33",
"-4.51",
"-3.3",
"6.65"
],
[
"BAT/USD",
"0.946",
"unknown",
null,
null,
null,
null
], [
"FUN/USD",
"0.000",
"0.00",
0,
0,
0,
0
] ]
}
Normally the json should be all String. I can handle the nill/null but I dont know how to handle if its Int/Double.
If it is a Int/Double I want to replace the value with "unknown".
This is my code so far:
struct JsonMaintableWebsocket {
let tpr: String?
let avgp: String?
let mcap: String?
let ppc7D: String?
let ppc12h: String?
let ppc4h: String?
let ppc24h: String?
init(json: [String?]) {
self.tpr = json[0]
self.avgp = json[1]
self.mcap = json[2]
self.ppc7D = json[3]
self.ppc12h = json[4]
self.ppc4h = json[5]
self.ppc24h = json[6]
}
static func fetchJsonWebsocketMaintable(json: Data) -> [JsonMaintableWebsocket] {
var jsonWebsocket: [JsonMaintableWebsocket] = []
do {
let jsonData = try JSONSerialization.jsonObject(with: json, options: []) as? [String: Any?]
if let data = jsonData!["data"] as? [[String?]] {
for d in data {
jsonWebsocket.append(JsonMaintableWebsocket(json: d))
}
}
}
catch let error{
print(error.localizedDescription)
}
return jsonWebsocket
}
}
Thanks a lot!
One way you could handle this would be to map over the array and check to see what kind of value is stored, and act accordingly. You might change this part:
if let data = jsonData!["data"] as? [[String?]] {
for d in data {
jsonWebsocket.append(JsonMaintableWebsocket(json: d))
}
}
to this:
if let data = jsonData!["data"] as? [[Any?]] {
for d in data {
let adjustedArray: [String?] = d.map({
//First, check to see if object is nil, and return nil if so
if $0 == nil {
return nil
//Check to see if value is string, and return string
} else if let stringValue = $0 as? String {
return stringValue
//Otherwise return "unknown"
} else {
return "unknown"
}
})
jsonWebsocket.append(JsonMaintableWebsocket(json: adjustedArray))
}
}
It would also be easy to handle Int or Double as Strings as well, by adding a couple more options:
if let data = jsonData!["data"] as? [[Any?]] {
for d in data {
let adjustedArray: [String?] = d.map({
if $0 == nil {
return nil
} else if let stringValue = $0 as? String {
return stringValue
} else if let intValue = $0 as? Int {
return "\(intValue)"
} else if let doubleValue = $0 as? Double {
return "\(doubleValue)"
} else {
return "unknown"
}
})
jsonWebsocket.append(JsonMaintableWebsocket(json: adjustedArray))
}
}

Swift Firebase Sort By Distance

I am trying to sort my array by distance. I already have everything hooked up to grab the distance's but unsure how to sort from closest to furthest from the users location. I've used the below code for MKMapItem's yet unsure how to apply to my current array.
func sortMapItems() {
self.mapItems = self.mapItems.sorted(by: { (b, a) -> Bool in
return self.userLocation.location!.distance(from: a.placemark.location!) > self.userLocation.location!.distance(from: b.placemark.location!)
})
}
Firebase Call
databaseRef.child("Businesses").queryOrdered(byChild: "businessName").observe(.childAdded, with: { (snapshot) in
let key = snapshot.key
if(key == self.loggedInUser?.uid) {
print("Same as logged in user, so don't show!")
} else {
if let locationValue = snapshot.value as? [String: AnyObject] {
let lat = Double(locationValue["businessLatitude"] as! String)
let long = Double(locationValue["businessLongitude"] as! String)
let businessLocation = CLLocation(latitude: lat!, longitude: long!)
let latitude = self.locationManager.location?.coordinate.latitude
let longitude = self.locationManager.location?.coordinate.longitude
let userLocation = CLLocation(latitude: latitude!, longitude: longitude!)
let distanceInMeters : Double = userLocation.distance(from: businessLocation)
let distanceInMiles : Double = ((distanceInMeters.description as String).doubleValue * 0.00062137)
let distanceLabelText = "\(distanceInMiles.string(2)) miles away"
var singleChildDictionary = locationValue
singleChildDictionary["distanceLabelText"] = distanceLabelText as AnyObject
self.usersArray.append(singleChildDictionary as NSDictionary)
/*
func sortMapItems() {
self.mapItems = self.mapItems.sorted(by: { (b, a) -> Bool in
return self.userLocation.location!.distance(from: a.placemark.location!) > self.userLocation.location!.distance(from: b.placemark.location!)
})
}
*/
}
//insert the rows
self.followUsersTableView.insertRows(at: [IndexPath(row:self.usersArray.count-1,section:0)], with: UITableViewRowAnimation.automatic)
}
}) { (error) in
print(error.localizedDescription)
}
}
First make these changes in your code
singleChildDictionary["distanceInMiles"] = distanceInMiles
Then you can sort it like this:
self.usersArray = self.usersArray.sorted {
!($0["distanceInMiles"] as! Double > $1["distanceInMiles"] as! Double)
}

Crash: libsystem_pthread.dylib: _pthread_wqthread

This is the number 1 crash of my application in the AppStore. Problem is I can't find a solution to this thing, because I can't reproduce it and don't know what is causing it. The crashlog says the following:
Thread 0.8 is didUpdateLocations.
I thought it might be in checkStealRange(), but I don't see something wrong with that.
func checkStealRange() {
var objectsWithdistance = [PFObject]()
stealobject = nil
print("checkin steal and setting stealobject to nil")
if nearbystreets.count != 0 {
for object in self.nearbystreets {
if let lon = object["lon"] as? Double, let lat = object["lat"] as? Double{
let locationStreet = CLLocation(latitude: lat, longitude: lon)
if let currentLocation = self.locationManager.location {
let distance = currentLocation.distance(from: locationStreet)
object["distance"] = distance
objectsWithdistance.append(object)
} else {
if self.lastlocationregionset != nil {
let distance = self.lastlocationregionset!.distance(from: locationStreet)
object["distance"] = distance
objectsWithdistance.append(object)
}
}
}
}
} else {
print("no nearby streets loaded to check for steal")
stealButton.isEnabled = false
return
}
if objectsWithdistance.count > 0 {
print("objectsWithdistance count:", objectsWithdistance.count)
let sortedArray = (objectsWithdistance as NSArray).sortedArray(using: [
NSSortDescriptor(key: "distance", ascending: true)
])
for object in sortedArray {
guard let street = object as? PFObject else { continue }
if let streetDistance = street["distance"] as? Double {
var allowedDistance = Game.steal.stealMinDistance +
Game.steal.stealDistancePerLevel * Double(Main().level())
if Main().getStealBoost() {
allowedDistance += 250
}
//print("distance:", streetDistance, "allowed:", allowedDistance)
guard let user = street["current_owner"] as? PFUser else { continue }
if user != PFUser.current() && streetDistance <= allowedDistance {
print("found steal")
self.stealobject = street
break
}
}
}
} else {
print("checkin steal failed")
stealButton.isEnabled = false
return
}
print("nearbystreet count:", nearbystreets.count)
if !self.timecheat && stealobject != nil {
stealButton.isEnabled = true
} else {
stealButton.isEnabled = false
}
}
Re-wrote the function using Parse localdata storage, and that fixed the trick.
func checkStealRange() {
stealobject = nil
let query = PFQuery(className: "SHStreets")
if let currentLocation = self.locationManager.location {
let userGeoPoint = PFGeoPoint(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
query.whereKey("geo", nearGeoPoint: userGeoPoint, withinKilometers: 5)
} else {
print("no location, returning from check steal range")
self.stealButton.isEnabled = false
return
}
query.fromLocalDatastore()
query.findObjectsInBackground { (objects : [PFObject]?, error: Error?) in
if error != nil {
print(error as Any)
self.stealButton.isEnabled = false
return
}
if objects == nil || objects!.count == 0 {
print("no nearby streets loaded to check for steal")
self.stealButton.isEnabled = false
return
}
if objects != nil {
for (index, object) in objects!.enumerated() {
guard let lon = object["lon"] as? Double else { continue }
guard let lat = object["lat"] as? Double else { continue }
let locationStreet = CLLocation(latitude: lat, longitude: lon)
if let currentLocation = self.locationManager.location {
let distance = currentLocation.distance(from: locationStreet)
//steal distance
var allowedDistance = Game.steal.stealMinDistance + Game.steal.stealDistancePerLevel * Double(Main().level())
if Main().getStealBoost() {
allowedDistance += 250
}
print("distance for street:" , index + 1, "is", distance, "allowed:", allowedDistance)
guard let user = object["current_owner"] as? PFUser else { continue }
if user != PFUser.current() && distance <= allowedDistance {
print("found steal")
self.stealobject = object
if !self.timecheat && self.stealobject != nil && !self.stealinprogress {
self.stealButton.isEnabled = true
} else {
self.stealButton.isEnabled = false
}
return
}
}
}
}
}
}

Resources