I want to store only one value per attribute in Xcode
If a new value is stored then I want to overwrite the first on, to keep only one value
one attribute = one value
Is there an option for that ?
Or should I delete existing value before storing the new value ?
(I learn core data, I may not use it right...)
If your data really only consists of single key-value pairs I would make use of UserDefaults and not core data. Specially if the data is rather simple and not "interacting on each other". The documentation can be found here UserDefaults doc, for examples see UserDefaults in Swift 4
Related
I have a flutter app that stores the data with objectbox but I need to change a class that has
// old:
int year;
// new:
double year;
ObjectBox does not support migrating existing property data to a new
type. You will have to take care of this yourself, e.g. by keeping the
old property and adding some migration logic.
On the objectbox page they say it's possible through migration logic.
Data is already stored with int and I need to migrate this data to double but I have not succeeded. Could someone help me with an example. Thanks
So as you've probably figured out already, doing a type migration while keeping the old data intact is not currently possible (as far as I'm aware). So you'll have to work around that, one convoluted way I can think of is to create a field with your new type, and then on app start, iterate through all your current saves and use year.toDouble() and move any current values in the year to the yearDouble. (Sorry on phone so no real code)
Then when you need to pull that value again, check the original year value, if it's empty, then read the year value from your new yearDouble
You could also just ignore the original iteration all together and just do your check when you use that value. So modify your current method to check if yearDouble is empty, then read original year.toDouble(), and then add the value to the yearDouble property at the same time
I am using a Closed range e.g let ageRange = ClosedRange(18...45)
What is the best way of saving this to Firebase DB? Do I need to save the lower and upper bound separately?
Thanks
The Firebase Database can only store JSON types.
Since ClosedRange is not a JSON type, you'll have to find a way to convert it back and forth.
Two common ways to do this:
Store it as a single string
Store its constituent parts
The first one is often easiest. But the second option has the advantage that you can query for its parts.
I have quite a broaden app in which I store lots of data. CLLocation ones, Arrays, CGPoints and so on. From time to time there's a need to save all these variables on the device, so I use UserDefaults. Yet, after some time of switching through ViewControllers and simply using the app (constant changing of data => constant saving it) I get to the point where everything runs slowly.
Now, suppose I have more or less three 2-dimensional Arrays and lets say 5 Ints I want to save to UserDefaults.
Every time I change one of the variables I "update" it by the same command:
UserDefaults.standard.set(data, forKey: "UDdata").
So every time I want the data to change, I change it through that particular comment. I guess every time I do it I should somehow get rid of the previous data for key UDdata and just then set it with the same key string? Information on that matter would be useful. Thanks in advance
The reason is you misusing the Userdefaults. They are to store small values like flags to detect first launches, preference and etc. Instead of storing data in Userdefaults just try to use CoreData or SQLite. Which is way more easier to use too.
CoreData :
https://www.raywenderlich.com/145809/getting-started-core-data-tutorial
SQLite :
https://www.raywenderlich.com/123579/sqlite-tutorial-swift
I have a Core Data, document-based, storyboard-backed, OS X project. Each document will have a specific calendar year and province/state it's operating in, so I made a Settings entity that asks for these values when the document is created. Since certain behaviours are based on these Settings, other custom NSManagedObject classes need to ask for them, and it doesn't seem right to execute an NSFetchRequest every time I need the value. However, I'm not sure where to copy the values to, outside Core Data.
AppDelegate - as far as I'm aware, there's only one per application, not per document, so this won't be appropriate.
Document - this seems best, but I'm not sure how to find the document again later, from inside an NSManagedObject. I tried defining a var year inside Document, then from the NSManagedObject calling theWindow.controller?.document?.year, but theWindow is nil if I don't set it myself. Also, theWindow is global, outside AppDelegate, so even if I do define it (in Document's makeWindowControllers() by theWindow = windowController.window), it again becomes per-app, so every time I call this line, I'm finding the year of the last-opened document (the last one to overwrite theWindow), not the active document. I'm not sure how to find a reference to the current window.
User Defaults - again, this is per-app, not per-document. My current solution uses these defaults, then redefines the defaults (with an NSFetchRequest) every time windowDidBecomeMain is called, so the current Document sets the User Defaults to its own Settings.
It seems unlikely I'm the only one to want settings per-document, but I can't seem to find any examples or previous questions on how/where to define these. Any help is greatly appreciated!
You're describing per-document metadata, and fortunately Core Data supports this kind of metadata. Each persistent store file (which is what NSPersistentDocument uses) can have its own metadata dictionary with whatever keys and values you need. It's kind of like user defaults except that it's part of the document. This data is part of the file but separate from the SQLite store that makes up the actual document data.
The metadata API is on NSPersistentStoreCoordinator. You get your coordinator in a document via self.managedObjectContext.persistentStoreCoordinator. There are a few methods there to read and write the document metadata. Save your year and province/state in the metadata, and then look it up and consult it when opening a document.
Core Data has its own metadata that you don't want to lose-- so when editing metadata, make sure to look up the existing metadata, modify it, and save the new data. Don't just assign a completely new metadata dictionary to the document.
I have an iOS application where I use coreData to store my "documents". They all share a common NSManagedObjectContext, and I frequently save the context.
I would like to keep track of the last modification date for the various "documents" (where each one is a separate NSPersistentStore) and store the date on a particular unique "root" object that each store has.
I could try to keep the modification time stamp up to date while the document is being modified, but it would be cleaner and more robust if I could just find out which persistent stores need saving at the time I am saving the context.
I can't find any way to detect if a persistent store needs saving. I can query the NSManagedObjectContext to see which managed objects need saving, although I can't find an easy way to see which store an object belongs to.
It seems like this is not such a strange thing to do and core data has all of the information that I am looking for, but I am having trouble finding an easy way to get access to that data.
Does anyone know of an easy way?
If I can't find an easier way, I will simply loop over the deleted / modified / inserted objects from the context, and write special code for each entity type to determine the store that the object belongs to.
Thanks in advance for any help!
Ron
[[managedObject objectID] persistentStore] is the persistent store you're looking for (or possibly nil if the object has not been saved yet).
The documentation suggests that it's nil if you've assigned it to a store but haven't saved; I'm not sure that this is true (and I don't see anywhere else where this info might be saved). I'd check it behaviour on 3.x, 4.x, and 5.0 beta if you have access to it.