Importing data via .csv file

I am relatively new to swiftUI and curious to hear what would be a good approach to import a csv file into an app.

The file has around 3000 rows and 12 columns. The file will be updated with new information frequently (at least once or twice a week) so it is important that when a user launches the app the most updated data is always available.

Appreciate any ideas or resources I can check to learn more about this.

Thank you
If you can switch to tab separated values, this is a relatively straightforward. Commas separate values, OTOH, are a royal pain. Unless your inputs are very constrained, it’s probably best to use a library for this. Unfortunately neither Swift nor Apple’s OSes have such a library built in, so you’ll need to look for a third-party one.

But, yeah, if you adopt tabs this problem is really easy (-:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thanks @eskimo,

Yes I can definitely switch to tab separated values.

What would be the best way to do that? My main question is how to make sure users always have the most updated data available after I refresh the file. From my understanding, if I just upload the tsv or txt file to xcode, users will have to download the app again every time I refresh the data (?)

Thanks,

Yes I can definitely switch to tab separated values.

Cool.

There’s two parts to this:
  • Distributing the data

  • Actually parsing the file

I was focused on the first but I think that was a mistake, and that you’re correctly focusing on the second.

From my understanding, if I just upload the tsv or txt file to xcode,
users will have to download the app again every time I refresh the
data

That’s correct. Given the frequency of data updates, you definitely don’t want to just embed this in the app.

I can think of two relatively simple approaches:
  • Place the data on a web server and download it using URLSession.

  • Put the data in your app’s CloudKit public database.

In both case you wouldn’t want to work with the raw .tsv file, but rather have the upload process parse that file, validate the data, and then upload it in a format that’s easier for the app to digest. For example, the web server case you could use a compression JSON file.

Oh, one other thing: Is this data confidential?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thanks @eskimo,

That makes a lot of sense. I was looking at an example where they import a file hosted in amazon web services (somehow I am not able attach the link) but as you probably guessed based on your question, the data is confidential. I could get around using a private dropbox link, but that would involve to somehow signing in with dropbox as part of the process.

I have done this with other languages but never in swift. Not sure if there is even a package for that?
Hi @Josedv

I'm working on an app with csv read functionality (tapping on a button to open share extension and read a file from Files/...). I managed to find the following in some other thread, which works(ish) for me (I'll explain the ish).

I totally agree with @eskimo that you should enforce some sort of formatting rule wherever csv files are generated (this was an ongoing pain at one of my previous jobs), and another pain was to verify the validity of the content, but I guess that's another conversation?

It's available from https://github.com/ahltorp/csvparser

I used it with Swift 5, and it reads my csv file properly.

My csv file was generated using commas as delimiter, nested data as header/detail report style and column order as original.

So the ish part, it generates an array of arrays of String ([[String]]), with the first item being the titles for each column (this is particularly useful because this determines the number of columns too, even if one of the rows in that column has an invalid value, I think).

I had to create some sort of verifier (to determine whether we have correct number of Strings in each row), and an adaptor for iterating through each inner array items and create my objects from it.

I guess the above link can be further improved by compiling data received into dictionaries/json which can then be converted to Data, and that data can then be provided in a generic form.

I'll try to do that and put it on Github, but this should unblock you (not sure if it's any longer relevant).

Best wishes, and if you have any further updates, please let me know
Importing data via .csv file
 
 
Q