简体   繁体   English

将临时数据保存在userdefaults中?

[英]Saving temporary data in userdefaults?

I've got views on my screen that are placed on the screen based on firebase data. 我有基于Firebase数据放置在屏幕上的视图。 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. 最初将视图植入屏幕时,它会从Firebase中保存的x和y中提取视图,并使用该视图将自身放置在屏幕上。

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. 视图是可拖动的,因此我希望在用户拖动视图,离开页面并返回视图时不再从旧的Firebase数据中拉出,而是从本地保存新的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. 在我做之前,我只是在touchbase上更新了Firebase上的x / y,但这对于许多同时执行此操作的用户来说是不切实际的,而且似乎没有必要。

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: 我的超级草率想法是,当触摸在这些视图之一上结束时,通过执行以下操作将x / y保存在userdefaults中:

1) Each view has a firebase UID tied to it, so grab that 1)每个视图都有一个绑定的Firebase UID,所以抓住

2) grab the X and Y positions of the button on touches end 2)在触摸端抓住按钮的X和Y位置

3) Have the key name be the String(UID + "xCoord") and one for Y as well and save the values under there 3)将键名设置为String(UID +“ xCoord”),将键名也命名为Y,然后将值保存在该名称下

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. 4)当我在寻找要绘制的视图的x / y值时,请检查是否为该UID的Xcoord + Ycoord设置了用户默认设置,否则请转到firebase。

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). 然后清理我的用户默认设置,我可以检查一下是否存在我将再次加载的视图的UID,如果没有,我可以清除这些坐标(不确定是否有必要)。

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. 您已经基本描述了macOS如何保存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:) . 您可以使用NSValue(cgPoint:)轻松将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. 我可能会在属性名称上加上一些前缀(例如“ windowPosition:”),而不是仅使用UID,但这并不重要。 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. 这几乎是用户默认设置的目的,尤其是在iOS用户无法直接与其交互的情况下。 Storing small pieces of data between launches of the program is its whole point. 在程序启动之间存储少量数据是其重点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM