Saving temporary data in userdefaults? - ios

I've got views on my screen that are placed on the screen based on firebase data. When the view is originally planted on the screen it pulls from the saved x and y from firebase and uses that to place itself on the screen.
The views are draggable and so I'm wanting when the user drags the views, leaves the page and comes back for the views to no longer pull from the old firebase data but some locally saved new x /y. Before what I was doing was just updating the x /y on firebase on touches end, but that's super impractical for lots of users doing that at once plus it just seems unnecessary.
The super sloppy idea I had was saving the x / y in the userdefaults when the touches end on one of these views by doing something like:
1) Each view has a firebase UID tied to it, so grab that
2) grab the X and Y positions of the button on touches end
3) Have the key name be the String(UID + "xCoord") and one for Y as well and save the values under there
4) When I'm looking for the x / y values for the view to be drawn in check to see if there's a userdefault set for that UID's Xcoord + Ycoord, otherwise go to firebase for it.
Then to clean up my userdefaults I could check to see if there's any UID the exists for views I'll ever load up again, and if not I can clear out those coordinates (not sure if that's even necessary).
Is this an abysmal way of doing this? Is there a better way to do it? I'd rather not get into core data because I've avoided it like the plague and this seems simple enough to not need it.
Any ideas on how to make this better?

You've basically described pretty much how macOS saves the locations of windows. The feature there called "autosave." User defaults is a fine place to put this.
I'd map just one property for the window, rather than two. You can easily store a CGPoint in user defaults with an NSValue(cgPoint:). I'd probably prefix something on the property name (like "windowPosition:") rather than just using the UID, but it probably doesn't matter that much. You could also store all window locations in a single property that stores a dictionary. Really, whatever is reasonably convenient. This is more-or-less what user defaults are for, especially in iOS where the user can't directly interact with them. Storing small pieces of data between launches of the program is its whole point.

Related

react-native Mark items as they leave the screen in scroll view

I am writing an iOS app (just iOS for now, so I don't need to consider other platforms at the moment) in React Native.
I have a screen in my app that is a ScrollView of items that was retrieved from a server and I'd like to mark each item as "read" as it passes out of the top of the screen for the first time (i.e. mark as read on scroll type functionality). Once an item has been marked as read there you can mark it as unread but through other actions not related to scrolling.
I cannot for the life of me figure out a good way to do this. Ideally the items themselves would be able to detect whether or not they have disappeared off the top of the screen and just update the server that way, but I can't seem to find if that's possible (I easily could have missed something in the docs but I don't think so).
At the moment my solution is calculate how far down the ScrollView is, divide that by the height of each item (which is static for now... I don't know what I'll do when it becomes not static, if ever), and that's how many items I need to mark as read. At that point I do logic to determine if the local item has already been marked as read and if not I update the local item and send an update to the server.
A previous solution was to just update the server on each item, but that seemed like it got out of hand too quickly because you can scroll pretty fast and each item needs to be marked as read accurately.
The server api calls are idempotent, so sending multiple updates for the same item, while not great, is also not the end of the world. Also, I am running this in the emulator on my Mac and I haven't yet tested it with a real device (I have one, but I am still in kind of early stages of development).
I am happy to provide any other information needed!
The onViewableItemsChanged prop will return a list of items who's visibility has changed. Keep in mind that this visibility is decided by the viewabilityConfig prop
https://reactnative.dev/docs/sectionlist#onviewableitemschanged

TodayViewController recreated every time Notification Center is shown

When trying to develop a Today Extension for iOS 8 I found a weird issue. It would seem that the View Controller I'm using is being recreated every time the user opens Notification Center. This means that there is no data retention and hence no way for me to know if the extension needs to update or not.
The main reason for wanting to do this is that I want to preserve the height of the widget (Using preferredContentSize). The problem is that when the view loads this value always gets reset which leads to the widget jumping around in size. The Stocks widget is an example of how it's supposed to work and they've obviously solved it so there has to be some way of retaining the data between different "sessions". I can also tell from the debugger that the process continues to run, so it's just down to something in Apple's code forcing the View Controller to be recreated.
My question is, is this a bug? Or is it based on some setting or some property that I'm missing? Or is it maybe just the way it's supposed to work? If so, what is the workaround?
That's the way they work. Save your information to the device so you can reload it the next time you launch, along with the update time so you know if it's out of date. For small amounts of data, just use NSUserDefaults. For larger amounts save to a file, CoreData, sqlite, etc.

how to implement the same pattern

I'm new to ios, and I've already got my design ready, now I don't know is there a best practice for me to do the following thing:
I have 1000 cards, each of it is like this:
And when I slide right, it will get the next one, I slide left it will back to the previous one
I can implement this now, but I can only implement this one by one for 1000 times.
In addition, when I click the middle of the card, it will flip horizontally, then show the translation. it's the second kind of card with different language and color.
So is there any hint for me? any loop can do it?
update
About data-drive, so I thought about database, and I search the google that there's four way of persistence in ios,
I decide to choose sqlite, is it a wise choice?
Reason:
I'm familiar with mysql, and use sqlite in rails before
sqlite is not too big.
I need to save original sentence, translate sentence, saved or not flag, additional story.

iOS app as a staged play

This may not be a unique question, but I don't know how to phrase it because my Google skills are insufficient.
I am writing an app framework and I am spending a lot of time writing a system to store the positions and other properties of UIView elements on the screen.
What I want to know is whether something like this already exists, since even though my system works well, I am concerned about memory usage with a large number of elements.
Basically, I subclassed UIControl and added a "state" property that stores position, alpha, colorscheme data and all forms of transforms that apply to a certain state in the interface. This is akin to actors positioned on a stage. When a scene change happens (a button is pressed, or something) the actors (UIViews) know exactly where to be and how to look from stored data.
This means that with a single button press and NSNotification, I can broadcast a simple integer that identifies the state to be in, and all necessary properties will be animated accordingly.
Am I wasting my time? Does something like this already exist. It does not appear to be included in the tools that Apple provides.

How do I make a Deep Copy of a UIElement?

So I have a printing component that serves a Silverlight application. Other modules in this program have the ability to signal the printing component and pass it a UIElement, which the printing component will then draw to the screen. All well and good. The problem arises when I try to manipulate the UI Element in order to better format it to fit the user's selected paper size or anything to that effect; it seems that the UI element that is passed in is frequently the exact same instance of the one on the screen, and the screen element changes itself to match the 'print-only' changes I have made. For now, I can manually save the previous values, make my changes, and restore the previous values, but it would be easier/more robust/more efficient/more flexible if I had a way to, given the UI element, make a copy of the element, and manipulate that freely, without worrying about alterations or state on the original UI element. How can I programatically copy an instance of a UI element such that I have another instance with the same visual appearance?
I know 2 ways you can try:
Save the object to a xaml string and recreate it from it.
(XamlWriter.Save and XamlReader.Parse)
Save the object with the serializer to a memorystream and recreate it from that - it is possible that not all objects are marked serializable so the other option might be the one to use.
It might seem a bit much - but there are not so many ways to create a deep copy - and not any standard c# method that I know of.

Resources