My function just keeps looping, is it something to do with firestore? - swift

My function is just looping at the print statement and not going into the ref db.collection Am I doing something wrong here? Thank you for any help. Im not really sure where to go, is this the best way to loop throw most of the data
func rat(){
var x = 0
while x != self.uniqueNumber{
let db = Firestore.firestore()
let indexString = String(x)
print(x)
let ref = db.collection("leaderboard").document(indexString)
ref.getDocument { (document,error) in
if error != nil{
print("cant get data")
}
if document != nil && document!.exists{
if let documentdata = document?.data() {
self.check = documentdata["points"] as! Int
self.uid = documentdata["uid"] as! String
x = x+1
print(x)
print("The user's ID was\(self.uid)")
if (self.check > self.firstPlace.placePoints){
self.secondPlace.placePoints = self.firstPlace.placePoints
self.secondPlace.placeNames = self.firstPlace.placeNames
self.firstPlace.placePoints = self.check
self.firstPlace.placeNames = self.uid
print("First Place was set to:\(self.firstPlace.placeNames)")
}else if(self.check < self.firstPlace.placePoints && self.secondPlace.placePoints < self.check){
self.thirdPlace.placePoints = self.secondPlace.placePoints
self.thirdPlace.placeNames = self.secondPlace.placeNames
self.secondPlace.placePoints = self.check
self.secondPlace.placeNames = self.uid
print("Second Place was set to:\(self.firstPlace.placeNames)")
}else if(self.check < self.secondPlace.placePoints && self.thirdPlace.placePoints < self.check){
self.thirdPlace.placePoints = self.check
self.thirdPlace.placeNames = self.uid
print("Third Place was set to:\(self.firstPlace.placeNames)")
}
}
if(x == self.uniqueNumber!-1){
print("This is the escape out")
self.setGuys()
}
}
}
}
}

Maybe you can try move the while x != self.uniqueNumber to inside the getDocument
func rat(){
var x = 0
let db = Firestore.firestore()
let indexString = String(x)
let ref = db.collection("leaderboard").document(indexString)
ref.getDocument { (document,error) in
if error != nil{
print("cant get data")
}
while x != self.uniqueNumber {
if document != nil && document!.exists{
if let documentdata = document?.data() {
x = x+1
}
if(x == self.uniqueNumber!-1){
self.setGuys()
}
}
}
}
}

Related

updating multiple firestore collections in loop

i'm trying to update multiple collections, the below code accessing getDocument only on the last item in the array, i tried to solve the issue by putting the getDocument in new function and using completion handler and dispatchGroup.
pp.order .forEach {singleOrder in
let dispatchGroup = DispatchGroup()
let zz = Order.init(data: singleOrder)
if (zz.isH == true){
let ww = db.collection("oo").document(zz.ComRefParentID)
dispatchGroup.enter()
ww.getDocument { (snap, error) in
guard let data = snap?.data() else { return}
let orderCpm = data["order"] as? [[String: Any]]
var uu:[[String : Any]] = []
var counter = 0
while(counter < orderCpm!.count){
let newArrayElement = OrderC.init(data: orderCpm![counter])
if(newArrayElement.ComRef == zz.ComRef){
let isOffered = newArrayElement.isOffered
let ComRef = newArrayElement.ComRef
let isAcc = true
let isActive = false
let theO = newArrayElement.theO
let editedElement = OrderC.init(ComRef: ComRef, theO: theO, isOffered: isOffered, isAcc: isAcc, isActive: isActive)
let toAdd = OrderC.modelToData(user: editedElement)
uu.append(toAdd)
}else{
let toAdd = OrderC.modelToData(user: newArrayElement)
uu.append(toAdd)
}
counter += 1
}
ww.updateData([
"order": uu
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
dispatchGroup.leave()
print("Document successfully updated")
}
}
}
}
}
}

Set a global variable from within GeoCoder function Swift

I have a global variable "filterError" that is initially set to false before a switch statement. When the switch statement occurs, it will go through a dictionary, and for each key it will do a unique logic check on the value. If that value does not match certain criteria then the Boolean variable filterError will be set to true.
When the switch statement is complete, if filterErrror is false then the an action will occur like so:
//...switch statement...
if (filterErrror == false) {
//do something.....
}
The issue I am having is with the geocoder function which is a case in the switch statement. If the switch variable key = "area" then the below is executed:
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, error) in
guard let placemarks = placemarks,let location = placemarks.first?.location
else { print("Error in finding location") return }
let distanceInMeters = location.distance(from: coordinateSelf)
print("The distance between property and user in miles is: \(distanceInMeters/1609)")
distanceInMiles = distanceInMeters/1609
var radius = Double()
if searchRadius == "this area only" {
radius = 0.49999999999
} else {
radius = Double(Int(searchRadius)!)
}
if (radius < distanceInMiles) {
filterError = true
}
}
However, the filterError is still false after the switch statement has completed because the boolean value is not being changed. How do you set filterError to true from inside the geocoder?
This is the full code:
for filter in self.activeFilters {
switch filter.key {
case "listingType":
if(filter.value[0] != propertiesFirestore[i]["listingType"]![0]){
filterError = true
}
case "minBedsBound":
if(filter.value[0] != "No min. beds") {
if(filter.value[0].filter("01234567890.".contains) > propertiesFirestore[i]["bedroomCount"]![0]) {
filterError = true
}
}
case "maxBedsBound":
if(filter.value[0] != "No max. beds") {
if(Int(filter.value[0].filter("01234567890.".contains))! < Int(propertiesFirestore[i]["bedroomCount"]![0])!) {
filterError = true
}
}
case "minPriceBound":
if(filter.value[0] != "No min. price") {
if(Int(filter.value[0].filter("01234567890.".contains))! > Int(propertiesFirestore[i]["price"]![0])!) {
filterError = true
}
}
case "maxPriceBound":
if(filter.value[0] != "No max. price") {
if(Int(filter.value[0].filter("01234567890.".contains))! < Int(propertiesFirestore[i]["price"]![0])!) {
filterError = true
}
}
case "includeSharedOwnership":
if(filter.value[0] != propertiesFirestore[i]["sharedOwnership"]![0]) {
filterError = true
}
case "includeRetirementHomes":
if(filter.value[0] != propertiesFirestore[i]["retirementHome"]![0]) {
filterError = true
}
case "area":
print(userlatitude)
print(userlongitude)
let address = "\(propertiesFirestore[i]["area"]![0]), \(propertiesFirestore[i]["postcodePrefix"]![0])"
var propLat = String()
var propLong = String()
var distanceInMiles = Double()
var searchRadius = self.activeFilters["searchRadius"]![0]
let coordinateSelf = CLLocation(latitude: Double(userlatitude) as! CLLocationDegrees, longitude: Double(userlongitude) as! CLLocationDegrees)
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, error) in
guard
let placemarks = placemarks,
let location = placemarks.first?.location
else {
print("Error in finding location")
return
}
let distanceInMeters = location.distance(from: coordinateSelf)
print("The distance between property and user in miles is: \(distanceInMeters/1609)")
distanceInMiles = distanceInMeters/1609
var radius = Double()
if searchRadius == "this area only" {
radius = 0.49999999999
} else {
radius = Double(Int(searchRadius)!)
}
if (radius < distanceInMiles) {
filterError = true
}
}
case "keywords":
print("Filter keywords are: \(filter.value)")
for j in 0..<propertiesFirestore[i]["keywords"]!.count {
propertiesFirestore[i]["keywords"]![j] = propertiesFirestore[i]["keywords"]![j].lowercased()
}
for keyword in filter.value {
if propertiesFirestore[i]["keywords"] == nil {
filterError = true
} else if ((propertiesFirestore[i]["keywords"]!.contains(keyword.lowercased()))) {
print("found keyword")
} else {
filterError = true
}
}
default:
print("Unknown filter: \(filter.key)")
}
}
if filterError == false {
filterProperties.append(propertiesFirestore[i])
} else {
print("FITLER CATCHOUT")
}

Swift - some mp3 file metadata return nil

I have code like below. For some file I got metadata like artist, title and other without any problem. For other files metadata list is nil but when I check metadata in editor like Tagger - title and other metadata exists. Furthermore when I change metadata in external editor for at least one key - my code starts work properly.
Could someone explain me where I make mistake ?
static func getBookInCatalog(url: URL) -> Book {
let book = Book(url: url)
let isDir: ObjCBool = false
var directoryContents = [URL]()
var totalTime: CMTime?
var size: UInt64 = 0
var chapters:Int = 0
do {
directoryContents = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [])
} catch let error as NSError {
print(error.localizedDescription)
return book
}
for item in directoryContents {
if !isDir.boolValue {
let result = appDelegate.fileTypes.filter { $0==item.pathExtension.lowercased() }
if !result.isEmpty {
chapters += 1
let fileSize = (try! FileManager.default.attributesOfItem(atPath: item.path)[FileAttributeKey.size] as! NSNumber).uint64Value
size += fileSize
let playerItem = AVPlayerItem(url: item)
let metadataList = playerItem.asset.commonMetadata
let asset = AVURLAsset(url: item, options: nil)
let audioDuration = asset.duration
if let _ = totalTime {
totalTime = totalTime! + audioDuration
} else {
totalTime = audioDuration
}
for metadata in metadataList {
guard let key = metadata.commonKey, let value = metadata.value else{
continue
}
switch key {
case "albumName":
if book.title == nil || book.title == "" {
book.title = (value as? String)!
}
case "artist":
if book.author == nil || book.author == "" {
book.author = (value as? String)!
}
case "artwork" where value is NSData:
if book.image == nil {
book.image = UIImage(data: (value as! NSData) as Data)
}
default:
continue
}
}
}
}
}
if let imageInsideCatalog = getImageFromFolder(url: url){
book.image = imageInsideCatalog
}
if book.title == nil {
book.title = url.deletingPathExtension().lastPathComponent
}
book.chapters = chapters
book.totalTime = totalTime
book.size = size
return book
}
MP3 meta data "standards" have gone through several major iterations over the years (see http://id3.org) . Your editor may be able to read older formats (that AVURLAsset may not support) and save them using the latest/current standard which would make them compatible after any change.

How do you update a users profile settings using firebase and swift?

I am trying to update a users email and full name. This is my code:
func saveTapped() {
var performSegue = false
if updateEmail.text == "" && updateFullName.text == "" {
self.cleanUrCodeRohan("Please fill in one or more of the missing text fields that you would like to update.")
}
if updateEmail.text != "" {
let user = FIRAuth.auth()?.currentUser
user?.updateEmail(updateEmail.text!) { error in
self.ref.child("users").child(self.currentUser).child("email").setValue(self.updateEmail.text!)
}
let emailUpdateRef = FIRDatabase.database().reference().child(currentUser).child("email")
print(emailUpdateRef)
emailUpdateRef.setValue(self.updateEmail.text)
performSegue = true
}
if updateFullName.text != "" {
let user = FIRAuth.auth()?.currentUser
if let user = user {
let changeRequest = user.profileChangeRequest()
changeRequest.displayName = self.updateFullName.text!
}
performSegue = true
}
if performSegue == true {
self.navigationController!.popViewControllerAnimated(true)
}
}
I am able to update the email under authorization but not under the database. Any help would be appreciated.
If JSON tree is something like this:-
appName{
users :{
userID :{
email : "..",
username : ".."
}
}
}
Use this Code to update your node's child value's:-
func saveTapped(){
if ((updateEmail.text != "" || updateFullName.text != "") && (updateEmail.text != nil || updateFullName.text != nil)){
let userRef = FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid)
if let new_Email = updateEmail.text as? String{
FIRAuth.auth()!.currentUser!.updateEmail(updateEmail.text!) { error in
if error == nil{
userRef.updateChildValues(["email" : new_Email ], withCompletionBlock: {(errEM, referenceEM) in
if errEM == nil{
print(referenceEM)
}else{
print(errEM?.localizedDescription)
}
})
}
}else{
self.cleanUrCodeRohan("Email couldn't be updated in auth")
}
}
if let new_Name = updateFullName.text as? String{
userRef.updateChildValues(["username" : new_Name ], withCompletionBlock: {(errNM, referenceNM) in
if errNM == nil{
print(referenceNM)
}else{
print(errNM?.localizedDescription)
}
})
}
}else{
self.cleanUrCodeRohan("Please fill in one or more of the missing text fields that you would like to update.")
}
}

Sorting mixed String-Int Strings numerically as the primary order, then alphabetically in swift

For a String which have both String and Int values (one of each) is it possible to do simple sort that will give the items ordered in numerical order as the primary order and alphabetical as the secondary order
var nameArray = ["Dave7", "Bob8", "Cathy9", "Henry10", "Susan10", "Pat11", "Steve12", "Dan12", "Ken1", "Sean2", "Howard3", "Dixie3", "Newman5", "Billy6"]
var sortedNameArray = nameArray.sort { $0.compare($1, options: .NumericSearch) == .OrderedAscending }
print(sortedNameArray) // gives the following:
Don't want this -> ["Billy6", "Bob8", "Cathy9", "Dan12", "Dave7", "Dixie3", "Henry10", "Howard3", "Ken1", "Newman5", "Pat11", "Sean2", "Steve12", "Susan10"]
Even though .NumericSearch was used the result is alphabetical.
I was able to get the desired result using a custom binary tree. Which gives the results:
Ken1 Sean2 Dixie3 Howard3 Newman5 Billy6 Dave7 Bob8 Cathy9 Henry10 Susan10 Pat11 Dan12 Steve12
But is there a simpler solution?
extension String {
var integerValue: Int? {
return Int(self)
}
}
func extractValueFromString(theString:String)->Int{
var catNumber: [Character] = []
//print("theString \(theString)")
for character in theString.characters{
var characterString = String(character)
if var value = characterString.integerValue { //if we don't check program crashes
//if numberSet.contains(Int(String(character))!) { //another way to check but redundant here
catNumber.append(character)
//print(catNumber)
// }
}
}
let numberString = String(catNumber)
return Int(numberString)!
}
class Node{
//nodes now only arrange strings
var data = ""
var value = Int()
var left:Node?;
var right:Node?;
deinit {
//print("deleting \(data)")
// print("node deleted")
}
init(data:String){
self.data = data;
//print(data)
}
}
class binaryTreeSort{
var root:Node?
init(){
}
deinit {
//print("tree deleted")
}
func getRoot()->Node{
return root!
}
func insertNewValue(data:String){
let newNode = Node(data:data)
var node:Node? = root
if (node == nil){
root = newNode
}
while (node != nil) {
let currentValue = node?.data
if currentValue == ""{
node?.data = data
return
}
if currentValue == data {
//we don't want duplicates.
return
}
if extractValueFromString(currentValue!) < extractValueFromString(data) {
if (node!.right != nil) {
node = node!.right
//print("Going Right at data \(node!.data)")
}else{
node!.right = newNode
//print("Going New Right at data \(node!.data)")
return
}
}else if extractValueFromString(currentValue!) == extractValueFromString(data){
if currentValue < data {
if (node!.right != nil) {
node = node!.right
//print("Going Right at data \(node!.data)")
}else{
node!.right = newNode
//print("Going New Right at data \(node!.data)")
return
}
}else{
if (node!.left != nil) {
//print("Going Left at data \(node!.data)")
node = node!.left
}else{
node!.left = newNode
//print("Going New Left at data \(node!.data)")
return
}
}
}
else{
if (node!.left != nil) {
//print("Going Left at data \(node!.data)")
node = node!.left
}else{
node!.left = newNode
//print("Going New Left at data \(node!.data)")
return
}
}
}
}
func inorderPrint(baseNode:Node){
if(baseNode.left != nil)
{
inorderPrint(baseNode.left!);
//print(" \(baseNode.data)")
}
print("\(baseNode.data)")
if(baseNode.right != nil)
{
inorderPrint(baseNode.right!)
//print(" \(baseNode.data)")
}
}
func reverseOrderPrint(baseNode:Node){
if(baseNode.right != nil)
{
reverseOrderPrint(baseNode.right!)
//print(" \(baseNode.data)")
}
print("\(baseNode.data)")
if(baseNode.left != nil)
{
reverseOrderPrint(baseNode.left!);
//print(" \(baseNode.data)")
}
}
}
var myBinaryTreeSort:binaryTreeSort? = binaryTreeSort()
for item in nameArray{
//print(item)
myBinaryTreeSort!.insertNewValue(item)
}
myBinaryTreeSort!.inorderPrint(myBinaryTreeSort!.getRoot())
print("---------------")
myBinaryTreeSort!.reverseOrderPrint(myBinaryTreeSort!.getRoot())
myBinaryTreeSort = nil //delete the tree
Use map to split the names into parts, sort to sort by number and name, and then map to restore the original:
func splitName(name:String) -> (String, Int) {
if let range = name.rangeOfCharacterFromSet(NSCharacterSet.decimalDigitCharacterSet()) {
return (name[name.startIndex..<range.startIndex], Int(name[range.startIndex..<name.endIndex])!)
} else {
return (name, 0)
}
}
print(nameArray.map(splitName).sort({ lhs, rhs in
if lhs.1 < rhs.1 {
return true
} else if lhs.1 > rhs.1 {
return false
} else {
return lhs.0 < rhs.0
}
}).map({ "\($0.0)\($0.1)" }))
Some other ways it could be done would be to maintain element 0 of the tuple as the full name (with numbers) and then the final map just becomes map({ $0.0 }) Depending on sizes, this may be more optimal than splitting the name each time it's compared.
If you have an array, you can sort with a custom closure.
For example:
nameArray.sort({extractValueFromString($0) < extractValueFromString($1)})
Will get you close. You just need to check if they are equal and return $0 < $1 instead.
Here's how I solved this, doing something similar to what #Lou-Franco alluded to:
func endInteger(word: String) -> Int {
if let range = word.rangeOfCharacterFromSet(NSCharacterSet.decimalDigitCharacterSet()){
let numberSubstring = word.substringFromIndex(range.startIndex)
return Int(numberSubstring) ?? 0
}
return 0
}
let sortedArray = yourArray.sort{endInteger($1) > endInteger($0)}