UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

High Level Core Data Binary Data Question

Forums > Swift

I'm working through an app to record a user's voice and save it to Core Data and then sync it to Cloudkit. I have it working now, but from parts of Apple's documentation about saving Blobs, a very complicated (for me) Core Data + Cloudkit project and other articles online I may be going about it incorrectly.

I think what the articles are saying is that I should be saving the binary data with "allows external storage" checked and then somehow saving a file reference. This is where I start to get confused:

  1. Do I need to put the file somewhere, get the reference and save the URL to Core Data?
  2. Should I continue saving with "allows external storage" checked? If so, how do I get the filepath when I use the file in the app?
  3. Related to #2, when a user accesses the file, is THAT when I extract the file, save it to a local path, and then use it now that it's kind of cached?

I think I'm looking for a high level explanation of the more correct way to save binary data into Core Data, have it sync to Cloudkit, and then retrieve that data and use it in the app.

Thanks for any help!

4      

Hi, I don't think you need to worry about the actual path. Core Data should take care of this and you should work with the actual data and not care from where it comes from.

In one of my apps I am using this external storage and I never touched the URL directly. When I needed to export this file, I used the data instance and write that to the URL.

3      

Interesting. My specific use case is getting the actual audio file back out and turning it into an AVAsset so I can play it with AVPlayer or just going straight from data -> AVAudioPlayer. Both players have different functions, and the main one I need is the addPeriodicTimeObserver so I can tell the UI to update a little progress bar. AVAudioPlayer doesn't really have that.

With the AVPlayer version... I think I need to go from file URL -> AVPlayerItem -> AVPlayer(playerItem:)

This lets me add a PeriodicTimeObserver.

With the AVAudioPlayer I can't add a PeriodicTimeObserver but I can go straight from Data -> AVAudioPlayer(data: data, fileTypeHint: "m4a")

Ok, I'm rambling now... I have it working both ways so I'll need to pick the best or re-evaluate how to build out the feature that lets the SwiftUI view know how long the progress is. The Core Data/Player piece is messy and I still don't quite understand where the data is... but I guess that will come with more time/testing.

3      

Hi , I'm about to start some testings for exactly this case: recording an audio file and storing it into Core Data somehow.

How can I give a URL to write to to an AVAudioFile instance when URLs are supposed to be managed by Core Data? Any advice from your experience?

Thanks!

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.