0

I am trying to store 30k users details in to core data. To achieve this I search and came up with a solution to have all the data in a file in CSV format. I am able to download and read CSV file to using following code:

func readCsvFile () {

    if let path = Bundle.main.path(forResource: "users", ofType: "csv") {

        if FileManager.default.fileExists(atPath: path){
            if let fileHandler = FileHandle(forReadingAtPath: path){
                if let data = fileHandler.readDataToEndOfFile() as? Data{

                    if let dataStr = String(data: data, encoding: .utf8){
                        print(dataStr)
                    }
                }
            }
        }
    }

}

but now the problem is that when i read entire data from file it causes memory issue. I need to read some portion and process core data storing. Again get back and continue data read from where I left and go on. My file has data as following:

Firstname,Lastname,Email,Phone,Title
Tanja,van Vlissingen-ten Brink,1,,Vulr(st)er
Berdien,Huismn Noornnen,2,,Filanager
Ailma,Ankit,3,,Vulr(st)er
Rzita,Salmani Samani,4,,Vulr(st)er
DeEora,Levaart,5,,Eerste Vulr(st)er
Kirsten,Veroor,6,,Vulr(st)er
Tristan,Haenbol,7,,Vulr(st)er
Manon,Bland,8,,Aankomend Vulrer
Naomi,Ruman,9,,Aankomend Vulrer

So I found that using NSFileHandler I can move or seek my file cursor to specific location in file, but it needs offset:

fileHandler = FileHandle(forReadingAtPath: path)
fileHandler.seek(toFileOffset: 10)

but I don't know how can I identify that I have read some specific number of lines, and now get back to next set of lines. Also NSStream is the way to read File but I haven't explored it.

James Z
  • 12,209
  • 10
  • 24
  • 44
Sagar In
  • 591
  • 1
  • 4
  • 8
  • Would it help to read the file into a String object directly using for instance `init(contentsOfFile path: String)` or a similar constructor? – Joakim Danielson Jul 13 '18 at 18:17
  • 1
    Seems strange to run out of memory reading such a small file into memory, 30k users at ~40 bytes per line is only 1.2MB – trapper Jul 16 '18 at 06:23

0 Answers0