What is the best way to store large file data into Coredata in iOS

81 Views Asked by At

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.

0

There are 0 best solutions below