9. SwiftData Storing Images and Bug Fixes

Поділитися
Вставка
  • Опубліковано 30 вер 2024

КОМЕНТАРІ • 44

  • @gakkieNL
    @gakkieNL 4 місяці тому +1

    Great series. Think I'm gonna hold of from SwiftData for now and continue on the CoreData + MVVM path. Storing and syncing Images works great already. I wouldn't know where to put al my logic if I start using SwiftData. I like MVVM for that, don't know yet what best practise there would be with SwiftData (MV pattern?) . Perhaps we'll see a series from you on that in the future!

  • @mario_luis_dev
    @mario_luis_dev 3 місяці тому

    On an app I'm currently working on the `@Attribute(.externalStorage)` is not working as expected, as I see the whole blobs (50KB+) being stored internally (they show as BLOBs, not images, but still I think those are large files regardless). The .default_SUPPORT folder is also not storing the images created from this data; I see all the large blobs in the persisted table. Not sure what's going on. Do you think that SwiftData possibly only respects the `.externalStorage` if the Data you're trying to save is larger (like 1MB+)? I saw a blog from Paul Hudson that mentioned something along these lines, but there's no in-depth discussion about it, just a brief mention of this issue.

    • @StewartLynch
      @StewartLynch  3 місяці тому +1

      That may very well be the case. I think I read that somewhere too.

  • @andrejkling3886
    @andrejkling3886 8 місяців тому +1

    Definitely amazing series 🎉 … thank you so very much for that ❤️ for your efforts and fantastic explanation 👌. Happy New Year 🥳 Stewart ! For You and Family 🎉🎉🎉 All the best!

  • @gustavomonge1785
    @gustavomonge1785 9 місяців тому +3

    Merry Christmas !!

  • @mario_luis_dev
    @mario_luis_dev 3 місяці тому

    another way to solve the Preview problem while leaving it in the Preview Contents folder is to add `#if DEBUG` directives on all previews

  • @dibjr
    @dibjr 9 місяців тому +1

    Thank you for another great video!

  • @joshgoble
    @joshgoble 9 місяців тому +1

    Great series! How would you go about resizing the image so that the data would take up less storage (other than jpg compression)? It seems like an area that you would never need the original full size image and having the image stored at a couple hundred kb rather than a few mb would be ideal?

    • @StewartLynch
      @StewartLynch  9 місяців тому +2

      Here is a gist of two extensions that I include in most of my projects that allow me to scale images. gist.github.com/StewartLynch/683d19ed0cd9fb600607da0b6f99aa49

    • @joshgoble
      @joshgoble 9 місяців тому

      @@StewartLynch thanks!

    • @joshgoble
      @joshgoble 9 місяців тому

      I tried out your code and it’s a life saver. Right now I have the image data being stored in my documents directory, but I plan on switching over to storing it in swift data like you’ve shown here some I’m already using Swift data to store the url. Great video

  • @jurgasbielinis2130
    @jurgasbielinis2130 8 місяців тому

    Thank you Stewart for an interesting video. Also I wanted to ask if you or someone have come across such SwiftData/iCloud app issues: 1. In Settings > iCloud > Manage Account Storage: clicking on the app to delete its iCloud data - does not open the next page - it only shows a spinner and that's it. Maybe I missed something in the code or setup to enable it? 2. Images with some data keep disappearing after app is restarted, sometimes they re-appear after awhile (might be related to connection) - but I would expect it to cache locally instead of removing/re-adding them. Any ideas would be really appreciated. Thank you very much in advance!

  • @RickyThompson-m9o
    @RickyThompson-m9o 8 місяців тому

    Super series! I like your teaching style. thanks for all your efforts. I am new to swiftUI and was wondering if you had any videos on using TabViews and SwiftData, specifically how to pass info from one screen to another, such as, tracking a character Model showing a List of characters on one screen then moving to another tab to enter data for the character/Item selected on the first screen into another Model. Would appreciate being pointed in the correct direction.

    • @StewartLynch
      @StewartLynch  8 місяців тому

      TabViews are not really meant for that Ricky. What you describe is either a NavigationStack or a modal sheet presentation. I have lots of videos on those topics. Send me an email (Links in my profile) if you want to discuss this further.

  • @profgallaugher
    @profgallaugher 7 місяців тому

    This was wonderful. I'm trying to store multiple images & am wondering how you'd handle a situation where you want the images to be stored externally (@Attribute(.externalStorage)) and also want deletions to cascade (@Relationship(deleteRule: .cascade)). Apparently putting both decorators above an array of data gives an error that they can't be composed. Thanks again for your work. It's great!

    • @StewartLynch
      @StewartLynch  7 місяців тому +1

      Good question John. I have not explored that yet

    • @profgallaugher
      @profgallaugher 7 місяців тому

      @@StewartLynch I'll have to explore this more. I'm finding that if I have a model with a single image, then save one image at a time to the model & query all saved values as an array of that model, I get the hidden .default_SUPPORT folder, BUT if I add a property to a SwiftData model that's an array of images and get this working, even with the .externalStorage - say something like this:
      @Attribute(.externalStorage) var imageArray: [Data?]
      if I look at the simulator's Application Support directory I don't see the hidden folder, so it's unclear if arrays of Data like this are even using .externalStorage. I may be completely missing something, but this is what I'm seeing right now. Will let you know if I discover more.

    • @StewartLynch
      @StewartLynch  7 місяців тому

      I just realized that I am doing this in my new app. What I do is create a new model for the image with a single property that is data. then in my model I have an array of SitePhoto with a cascade delete. It all seems to work for me. Follow up with a DM on Mastodon or X if you want to discus more.
      @Model
      class SitePhoto {
      @Attribute(.externalStorage)
      var data: Data?
      var image: UIImage? {
      if let data {
      return UIImage(data: data)
      } else {
      return nil
      }
      }
      init(data: Data) {
      self.data = data
      }
      }

  • @mario_luis_dev
    @mario_luis_dev 3 місяці тому

    this was a masterful series btw. Your work is very much appreciated Stewart ❤

  • @jousufjacob8251
    @jousufjacob8251 8 місяців тому

    Hi Stewart , thanks for you video , i have question here, if i want to fetch the image in the list of listview ,what should i do ?

    • @StewartLynch
      @StewartLynch  8 місяців тому

      What do you mean by fetch the image? I don’t understand your question

  • @gothefreakyway9509
    @gothefreakyway9509 8 місяців тому

    Hey, nice video !
    I got a question - is it possible to pick an Image but with fixed aspect ratio with the Photospicker ?
    So lets say I am choosing an Image in my library, but then I get like a frame(fixed ratio like 2:1) on that Image where I can decide which part with that ratio I want to load.
    Or do i have to load it like so and make it resizable and scale it then ?

    • @StewartLynch
      @StewartLynch  8 місяців тому +1

      I have not gone down that path myself. I do not think it is natively possible

    • @Grepsoft
      @Grepsoft 7 місяців тому

      You cannot do it directly from the photopicker but you could load the image onto a canvas and then draw a rectangle on it for the frame and then use UIGraphicsImageRenderer to grab the area under the rectangle.

  • @gakkieNL
    @gakkieNL 4 місяці тому

    Thanks! Keep up the great content!

  • @vmonzillo
    @vmonzillo 7 місяців тому

    Thank you Stewart! I wonder if there is a way to pick photos from the camera just like that

    • @StewartLynch
      @StewartLynch  7 місяців тому +1

      Funny you should say that. I am working on a video on that topic. Won’t be out for. month or so though.

    • @vmonzillo
      @vmonzillo 7 місяців тому

      @@StewartLynch Wow! Great minds think alike 😅. I said that because I've just made a little project using something like that. Can't wait to see what you have for us!

  • @AndrewDChristie
    @AndrewDChristie 6 місяців тому

    Thanks!

  • @AndrewDChristie
    @AndrewDChristie 7 місяців тому

    Thanks!

  • @RoxieS-pv4ke
    @RoxieS-pv4ke 9 місяців тому

    in my project i can add and save Video files in SwiftData

    • @StewartLynch
      @StewartLynch  9 місяців тому +1

      I don’t think you would want to save video files to a SQLLite database. I would just store the videos on device with an ID and then reference them in the database.

    • @joshgoble
      @joshgoble 8 місяців тому

      @@StewartLynch Great point about not wanting to save the video in the database, but that does bring up an issue regarding CloudKit. If you were to store the videos locally in your apps .documentdirectory, what would be the way to have those files synced with CloudKit. I did a few searches and wasn't able to find any information on syncing a app folder.

    • @StewartLynch
      @StewartLynch  8 місяців тому

      @joshgoble you would be better storing the video somewhere where it can be streamed and then just store the link

    • @joshgoble
      @joshgoble 8 місяців тому

      @@StewartLynch thanks for the reply Stewart. I guess to make this less theoretical, I’m working on an app that specifically needs to be able to have offline video capabilities. It’s great that the swift data that holds all the urls and images can be directly stored in CloudKit, but if you absolutely need to have the videos stored locally in your apps documents directory, is there any way you know of to tell CloudKit to include an entire folder? I realize that syncing may take a few minutes, but I’m not sure hot to go about doing that? Or would you go to something else for that kind of syncing? Something like firebase?

    • @The1tjc
      @The1tjc 8 місяців тому +1

      @@joshgoble Why not try saving the video file as shown for saving an image, as the video file is large it will be placed in external storage as Stuart shows. Then see if cloudKit can handle the sync. Maybe the CloudKit docs describe the limits on file sizes it can handle. Experimentation leads to knowledge.