|
Dear Community, I need your help. I am new to SwiftUI and I'm trying to implement a function that automatically retrieves data after a few checks when the app reloads. The data comes from an external JSON file stored inside the app using SwiftData. I'm attempting to either download new data (if it's not already present in the User model) or refresh existing data if certain values for myuser are different. In general, the app's database should monitor changes in the external JSON. I started with the first approach, adding an if check for new records, but the code is not working as expected (the function adds all records again and again). I appreciate any guidance or suggestions you can provide. Thank you! Example code.
|
|
One of the possible ways to arrange your code is like so. You can just copy paste to check the workflow. The URL fetch is working one.
|
|
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
You forgot to attach container in your @main entry
|
|
I believe a much simpler solution is to add to |
|
Dear @ygeras , I played with your code for understanding how it works, but not working for me. I tested as new project by copying and pasting the code into the ContentView. Based on your dummy JSON, I created two JSON files with the same name, users.json: 1) the original (29 items) and 2) with 28 records. Initially, I uploaded the JSON file with 28 items to my server, and the App successfully processed it. Later, I replaced it on the server with the file containing 29 records and pressed the 'Fetch Data' button in the app, expecting it to update and show the additional user. However, the app did not reflect the update and still showed only 28 users. Additionally, I tried using Context delete, but the app continued to display 28 users as it did after the initial upload. SwiftData records not changed as expected.
|
|
Listen, I have no idea what exactly you are doing without seeing the real code. In words in can be one story in reality the other. The code that I offered does not include all the logic but the one possible solution how it can be handled. Also all depends on what you downloaded with JSON containing 28 names and then replaced it with 29 names. If for example the file with 28 names contains Smith James, and then in another JSON file you add the same person, you won't have them 29 in your DB. As you are matching it by names, i.e. you download items and then check if the one with the same name already exists. If it does then that item won't be saved. You can also use not name but id, or any another unique property for that purpose. Once again i just modified my previous code a bit for more readability and also printing out the db store, if you have any sql db reader you can verify yourself that data persists in db and when you remove person it also removed from db, I just myself verified that serveral times. Also if I download again the item that was deleted again saved to db as a NEW ITEM and in turn appears on the screen. So it is something in the code you're playing around... just try to split it in managable chunks and try to isolate the issue when something goes wrong. Below modified code, but i did not change any logic in a big way that is different from the previous code. Only optimization with fetch count, which is really fast.
Watch out for number of users at the top of the seciton. |
|
Thank you for clarification. I use your code only. After replacing JSON file with higher number of names on server, SwiftUI don't find new names and don't add them. |
|
Check if JSON you receive is updated one. I am not an expert on server side, but maybe some issues with that... no clue. But as you can see from the code above you can delete data and after new fetch it is updated and saved. So logically if there are more items it should add them accordingly, as I do not see any issues that might stop them from updating... |
|
Thanks for replying. The server side is not involved. Here's a video of my process where you can see that SwiftData is not updating with new records after upload. Let me know if you have any insights or suggestions on how to fix this. |
|
The issue in how you update and what you do with your json. Using my pure code you can just change API to this string
you can increase limit up to 100, seems like there are 93 records in total, by default you get only 29, and you can verify that code is working properly this way and new items are downloaded as expected. |
|
As seen on video, initialy JSON file had 28 records, uploaded file with 29. I didn't exeed any limits. |
|
I think you should check how to update JSON on the back end and how to communicate it to front end. I doubt that folks replacing files this way. When backend is setup correctly as in dummyjson it works properly. Once again, without seeing you set up in details, it is difficult to say what is wrong. There are thousands of reasons that something may goes wrong. |
|
As an option try to use GitHub gists to keep 2 json files and use those API to download. I have just run the test this way. Everything works absolutely perfectly. So the way you communitcate json to your device is not configured in proper way. |
|
Tested files from another server, everything is working. Will use GitHub. Thank you for help! |
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.