Extremely high memory and CPU usage when loading parsed JSON data into Firebase in a loop function

This is my first question here, so it's easy on me!

I am a newbie encoder and am currently trying to loop through JSON, parse the data and back up the information on my Firebase server - using Alamofire to request JSON information.

Swift 4, Alamofire 4.5.1, Firebase 4.2.0

The process works, but not without an endless increase in device memory usage and up to 200% CPU usage. Through the comment lines, I allocated memory and CPU usage to the setValue line to load Firebase in my data push function, which iterates through a JSON database with an unknown length (by pulling a maximum of 1000 rows of data at a time - hence increasing offset values). The database from which I extract information is huge, and with increasing memory usage, the function flickers very slowly.

The function detects that it has detected empty JSON (end of results), and then either completes or parses JSON, loads the information into Firebase, increases the offset value by 1000 lines and then repeats itself with a new offset value.

var offset: Int! = 0
var finished: Bool! = false

func pullCities() {
  print("step 1")
  let call = GET_CITIES + "&offset=\(self.offset!)&rows=1000"
  let cityURL = URL(string: call)!
  Alamofire.request(cityURL).authenticate(user: USERNAME, password: PASSWORD).responseJSON { response in
    let result = response.result
    print("step 2")
    if let dict = result.value as? [Dictionary<String, Any>] {
        print("step 3")
        if dict.count == 0 {
            self.finished = true
            print("CITIES COMPLETE")
        } else {
            print("step 4")
            for item in dict {
                if let id = item["city"] as? String {
                    let country = item["country"] as? String
                   let ref = DataService.ds.Database.child("countries").child(country!).child("cities").child(id)
                        ref.setValue(item)
                }
            }
            self.finished = false
            print("SUCCESS CITY \(self.offset!)")
            self.offset = self.offset! + 1000
        }
    }
        if self.finished == true {
            return
        } else {
            self.pullCities()
        }
     }
  }

, , Firebase, - ? .

, :

  • , ( , 1 - , )

  • ( Xcode , "CFString ()" "__NSArrayM" - setValue )

  • ( , , - )

  • autoreleasepool ( , )

  • ( , )

!

UPDATE

Allocations (1000 ). , , , , Firebase dict, , -, , ? , , . - , !

Distribution chart

- , . , nodejs, . HTTP- javascript!

+4
2

-, , , , autoreleasepool {}, . ARC, , Swift, SO:

autoreleasepool Swift?

, .

0

, . .

env .

0

Source: https://habr.com/ru/post/1686347/


All Articles