Savinging and generating AVMetadataMachineReadableCodeObject Swift - swift

I'm having some serious issues here trying to save a AVMetadataMachineReadableCodeObject in Swift.
I'm using this library: https://github.com/yeahdongcn/RSBarcodes_Swift, but it has terrible documentation.
Basically, I can scan fine and can handle that scan and future scans to build a "Scan History" type screen... The problem comes in when I restart the application - my Scan History is empty (obviously). I need to save these scans locally somehow but I'm not sure how to do that.
I've tried saving the attributes of a scan but rebuilding it after app restart is proving to be just as tricky. I've tried saving them as custom objects (which would be ideal and is possible - except for the AVMetadataMachineReadableCodeObject part as I get errors like non-property etc)
I'm approaching the point where I think I should try something like Realm but I've never used it before.

You can't easily save AVMetadataMachineReadableCodeObject to Core Data, because it's not a supported type and does not conform to NSCoding. Switching to Realm won't make any difference, because it has the same restrictions (it can't just save an AVMetadataMachineReadableCodeObject). The reasons are similar-- in both cases AVMetadataMachineReadableCodeObject isn't a subclass of the appropriate type, and it's not one of the supported property types.
What you need to do depends on how exactly your scan history UI is supposed to look. Saving the AVMetadataMachineReadableCodeObject is almost certainly not the best approach, though (you might be able to add NSCoding through a Swift extension but it's probably the wrong answer even if it works). There are a couple of possibilities:
Save the data represented by the scanned code instead of the code itself. You get this from the stringValue property of AVMetadataMachineReadableCodeObject. When you want to show the scan history, generate new images to display. This is straightforward using Core Image-- COQRCodeGenerator, CIAztecCodeGenerator, CICode128BarcodeGenerator, and CIPDF417BarcodeGenerator are all built in.
Take a photo at the same time as you scan the image, and display the photo as the scan history entry. Include the stringValue of the scanned code in the UI so that people can see what the image represents.

Related

I get a warning in my console when load data from Coredata

i have a small problem with my code. When I load data from my CoreData I get warnings in my consol but I don´t know why. I´m not a professional programmer and also google couldn´t really help me so now I´m here.
I load my data like this:
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var data: [DataToLoad] = []
override func viewDidLoad(){
do{
data = try context.fetch(DataToLoad.fetchRequest())
}catch{
print("error")
}}
But when I load it like that the console says...
"2020-09-05 20:32:04.884728+0200 Test-Application[3850:348569] [general] 'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release"
...after I start the App.
So I mean everything is working fine, but where is my error that this massage appears.
I hope you can help me.
Sorry for my bad english I hope you can understand everything.
Have a nice day and thank you all.
The app is still working because it's a warning and not an error but it's good to not ignore warnings.
In your case, the warning occurs because NSKeyedUnarchiveFromData is deprecated. This means it's outdated and will be replaced in the future and the app might then stop working.
Kaira Diagne explains:
With iOS 12 Apple has started adopting NSSecureCoding across the entire platform. For Core Data this means that the default ValueTransformer, which uses NSCoding to transform a custom data type into a format that can be stored in the persistent store, at some point will change as well.
In your case NSSecureUnarchiveFromData instead of NSKeyedUnarchiveFromData in Core Data.
This means that the first thing we need to do is make sure that the data type of every transformable property in our data model conforms to secure coding.
(Link: NSSecureCoding and transformable properties in Core Data, 2020)
For your app to also work in the future you will have to dig somewhat deeper into the code and see where you are using the deprecated methods and classes and replace them.

How to get frame data in AppRTC iOS app for video modifications?

I am currently trying to make some modifications to the incoming WebRTC video stream in the AppRTC app for iOS in Swift (which in turn is based on this Objective-C version). To do so, I need access to the data which is stored in the frame objects of class RTCI420Frame (which is a basic class for the Objective-C implementation of libWebRTC). In particular, I need an array of bytes: [UInt8] and Size of the frames. This data is to be used for further processing & addition of some filters.
The problem is, all the operations on RTCVideoTrack / RTCEAGLVideoView are done under the hood of pre-compiled libWebRTC.a, it is compiled from the official WebRTC repository linked above and it's fairly complicated to get a custom build of it, so I'd prefer to go with the build available in the example iOS project; in my understanding it's supposed to have all the available functionality in it.
I was looking into RTCVideoChatViewController class and in particular, remoteView / remoteVideoTrack, but had no success in accessing the frames themselves, spent a lot of time researching the libWebRTC sources in official repo but still can't wrap my head around the problem of accessing the frames data for own manipulations with it. Would be glad for any help!
Just after posting the question I had a luck in finding the sneaky data!
You have to add the following property to RTCEAGLVideoView.h file:
#property(atomic, strong) RTCI420Frame* i420Frame;
In the original implementation file there is the i420Frame property but it wasn't exposed in the iOS project's header file for the class. Adding the property allows you to get view's current frame.
I'm still in search of a more elegant way of getting the stream data directly, without the need to look into remoteView contents, will update the answer once I find it.

Is it Possible to put the whole CoreData-Stack in a framework?

I have a very large Project and I am right in a refactoring session. At the beginning my idea sounds good to me, but now i am stuck and confused and near before a git rollback. You guys are my last chance.
We’ve done a GUI application that shows some data that are living in CoreData. A background Process is fetching data from time to time from the network and put them into the CoreData.
So I’ve got the request thing -> pushing into CoreData and <- getting it out onto the GUI.
That works for very good for us. It’s straight forward and nothing special. My Company is programming this application since January and the requesting and data part gets huge. We also have a 100% test coverage! \o/. Working in that project could be so nice… if… if the GUI things could be separated.
As time goes by I have to split the dev-team into two parts: one for the gui (and the several gui applications that all based on the same data model) The second one for the Request-and-CoreDate part. My Model interface is clean enough to have a nice entry point, so i thought i would be easy to move the Request and CoreData sections into a library-project in my Workspace.
hm, well. no!
I put the Kit.xcdatamodeld into the new library project. I generate all models and add all the code from the main application into the lib. I resolved every problem and start to move the first basic tests into the new project.
I put the managedObjectModel part from our ApplicationDelegate into a DataController in the new project. And changed the first call to use this managedObject from the new class.
let dc = DataController()
_ = GlobalScope(context: dc.managedObjectContext!)
When I run the first test that should store a simple entity than an error occurs in managedObjectModel:
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = NSBundle.mainBundle().URLForResource(" <LibName>Kit", withExtension: "momd")!
return NSManagedObjectModel(contentsOfURL: modelURL)!
}()
fatal error: unexpectedly found nil while unwrapping an Optional value
My Question is: Is it possible to do the whole CoreData stuff into a Framework? I wan’t have any data model or requests in my gui applications? The plan ist to include the framework and use the getXY() setXY() functions of my models. Also the whole network background fetching should be places in the framework.
But it seams that there is no mainBundle in a framework. So how to get the database the right way?
Thank you very much.
ps
First, Core Data is already a framework. You are looking to put your layer on top of Core Data into a framework. Is it possible? Probably, but it is not a great idea.
There is no value in putting a third (or more) of your application into a framework. It will not make anything cleaner or easier. In fact it will make debugging of your application harder.
Separate out the code using groups in Xcode and call it there. Trying to put things into separate Xcode projects (which is what you would need) would lead to more issues than it is worth.
BTW, there is no way to compile the model into the framework as that is an independent file. Therefore your primary application would need to embed the model file anyway.
You could explore creating your own custom CocoaPod like structure but again, it really is not worth it and maintainability will decrease.
The problem is this line:
let modelURL = NSBundle.mainBundle().URLForResource(" <LibName>Kit", withExtension: "momd")!
It's looking in the main bundle for that resource, which doesn't exist. You'll want to use bundleWithIdentifier or bundleForClass instead, like:
let modelURL = NSBundle(identifier:"com.whatever.yourFrameworkIdentifier").URLForResource...

Core Data Error

I am creating an application using Core Data. But When I run my app it crashes. It shows me this reason:
reason = "The model used to open the store is incompatible with the one used to create the store";
What should I do now?
I think you need to refer this link and refer to answer given by Grouchal in the link below:
What do I have to do to get Core Data to automatically migrate models?
If you need any further help please do let me know.
Hope this helps you.
It means that you created a model, run the app, modified the model (by adding or removing managed objects, renaming object properties and so on...) and reopened the app without migrating the data first. I still have to try this by myself but, you should definitely take a look here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreDataVersioning/Introduction/Introduction.html
You can just delete the app (icon) from the simulator or device. Then, rebuild and run.
As dave explained, you have modified the core data structure (the store). If you're using the standard template, in the new rebuild a new model (NSManagedObjectModel) will be created that is compatible with your newly modified store.
You will probably be making many changes to the core data structure (adding or deleting an entity or some property or relationship), and, each time you do, you can just delete the app from the simulator or device.

How to migrate NSUserDefaults in new release?

I have an iPhone app that stores some settings using NSUserDefault standardUserDefaults.
When I add new features to the app I need to add new default settings and so I have to migrate/upgrade the NSUserDefaults. For now, I just store the version number and check this when the app is launched. It has quickly become very messy as I have to add lots of if statements. I cannot assume that the user is just upgrading from the previous version but perhaps even a couple of versions before.
I like the way that CoreData seems to handle migrating table changes but I want to provide 2.2.1 SDK compatibility and of course CoreData is not the same thing as NSUserDefaults.
Any suggestions or best practices?
Hmm… I wouldn't "upgrade" the user defaults in this way, but instead query NSUserDefaults as usual, only if the default isn't set (objectForKey will return nil in that case), use a default value (which may then be written back to the user defaults to save time the next time). Since you'll have to do this every time a "new" default (i.e. one that didn't exist in 1.0) is read, I advise doing a wrapper class/file that does this, this way the code is written only once for each "new" default.
Also, while the question/problem is different, this answer works just as well in your case.