I am currently making an application in Swift. I was looking into Core Data to store the little bit of data I actually need to store. I want three (permanent number) of these objects stored within the phone at all times.
class Location {
var name = ""
var type = ""
var address = ""
var distance = ""
init(name:String, type:String, address:String, distance:String) {
self.name = name
self.type = type
self.address = address
self.distance = distance
}
}
These are locations of a sort that users can save within the app. Then they can click a labeled button that will load the text fields with the stored data. The user can update them from time to time if they need to. The issue is not how to save within Core Data or fetch it. The question is, how can I have these Entities/Attributes already stored into the app before anyone even runs / downloads it? I figured I could just create these saved locations at start and flag it whether or not this is the first run to eliminate the duplications. But shouldn't there just be a way to build the objects into the program from the start and just manage them from then on? Or is there an entirely different method I am over looking?
I have never used Core Data but it seemed like the way to go from the tutorial book I am reading. It, along with online resources, just state how to build and maintain Code Data from scratch. I could not find how to build a library in and manage it instead.
Related
im making a project for listening to episodes of a podcast as if they're real cassettes. The idea is that some info about the episode shown on the cassette model + it's audio file are stored in a class, then before putting the cassette in the player, being able to change the class resource the cassette takes from with buttons. My problem is that it doesnt let me change it on runtime, just set it on the ready function.
The way the cassette script loads the data:
onready var data = preload("res://MAGs/Season1/Ep1.tres")
The way im trying to change it:
func _ep_select():
data = load(<different cassette's data>)
If its impossible to change it on runtime, what should i use instead of this?
Currently I am saving game progress in a file in JSON format. Each time player completes the level a new entry will be added to JSON file as follow "level_N":{"score":234,"points":22},
this file will be saved at the end of Level complete. Consider scenario in which player reaches level 2345 or so, In that case saving to the file on level complete takes considerably longer time in some mobile devices. how to manage saving such a large amount of data ? do I have to use some other formats ? or do i have to save each level detail in separate file ?
PlayerPrefs is the easiest way to save data. It’s designed to handle basic data types (int, string, float) and works like a dictionary, so you can simply store JSON string as key-value pairs. And there is no size limit on iOS or Android (in webplayer it's limited to 1MB).
// read
PlayerPrefs.GetString(string key, string value);
// write
PlayerPrefs.SetString(string key, string value);
// load and update
const saveKey = "level_N";
Private void SaveProgress()
{
string saveValue = "your JSON string";
string loadValue = PlayerPrefs.GetString(saveKey);
if (!saveValue.Equals(loadValue))
{
PlayerPrefs.SetString(saveKey, saveValue);
PlayerPrefs.Save();
}
}
User data will be automatically written to disk during OnApplicationQuit(), but you may want to use PlayerPrefs.Save() in case your game crashes.
Having said that, saving a large amount of data to PlayerPrefs on mobile devices might be slow. So if you want to improve your game performance or need more space, you can use Application.persistentDataPath to save data to a public directory on the device. Similar to PlayerPrefs, data is not cleared when app is updated.
FileStream file = File.Open(Application.persistentDataPath + "/gameInfo.dat", FileMode.Open);
You can use PlayerPrefs class to save to save the local data.
In your case, you can trun your json to string then call PlayerPrefs.SetString to save/PlayerPrefs.GetString to get.
PlayerPrefs is always a good choice for local data managing but if you planning to add online features to your game, I recommend you to take a look at Firebase Realtime Database for sync json data with offline support. And let the firebase manage performance issues on mobile devices. Here you can see how to integrate Firebase Database to your Unity project easily.
I'm getting the above layout from Parse. What I want is vid 1, 2, and 3 to be in the same row; associated with same object ID. How can I do this? My ultimate goal is to easily retrieve 10 video dictionary's per user on a table view. Will any of this make a difference? I'm saving like this.....
videoDict = ["id":videoId, "title":vidTitleText, "description":vidDescription, "image":vidIMG]
let videoSave = PFObject(className:"UserVideos")
videoSave["user"] = PFUser.currentUser()!.username
videoSave["userObjectId"] = PFUser.currentUser()!.objectId
videoSave["vid\(saveValueLBL.text!)"] = videoDict
videoSave.saveInBackgroundWithBlock { (success, error ) -> Void in
if success == true
{
print("Succesfull")
}
}
Where you have let videoSave = PFObject(className:"UserVideos") you are creating a new videoSave object each time. you need to move that outside of your loop so that you're accessing the same object each time instead of making a new one. However, the way you currently have your code set up you'll run into problems, because each object can only have one synchronous action called on it (in this case, your save), so the second, third, maybe even all the way to the 10th save may not occur because it needs the first one to finish before the next one can be called. You need to create your object outside your loop, run the loop, then call the save at the end to make sure it isn't saving until all of the data is updated.
If this isn't all inside of a loop, you need to get the videoSave object back each time, perhaps by storing it onto your user, and then fetching it from the user object.
Put everything outside the loop and keep just the code below inside the loop:
videoDict = ["id":videoId, "title":vidTitleText, "description":vidDescription, "image":vidIMG]
videoSave["vid\(saveValueLBL.text!)"] = videoDict
From what I understand although I saved information in Parse as a Dictionary this is in fact an invalid data type. That's why I'm having trouble retrieving because Parse doesn't recognize the info.
I am developing a app for iOS using Flash, and I've created a LSO using the following code:
var so:SharedObject = SharedObject.getLocal("name");
so.data.user = enternamehere.text;
var result:String = so.flush();
I'm using a input text field so that users can enter their name. The instance name is "enternamehere".
I'd like to be able to call the information stored from the input text to appear on a different scene within the file.
So, a user enters their name on scene 3, and it appears on Scene 4 and the name is stored locally.
Any way of doing this?
Thanks
I'm building a WP7 app, and I'm now at the point of handling the tombstoning part of it.
What I am doing is saving the viewmodel of the page in the Page.State bag when the NavigatedFrom event occurs, and reading it back in the NavigatedTo (with some check to detect whether I should read from the bag or read from the real live data of the application).
First my VM was just a wrapper to the domain model
public string Nome
{
get
{
return _dm.Nome;
}
set
{
if (value != _dm.Nome)
{
_dm.Nome= value;
NotifyPropertyChanged("Nome");
}
}
}
But this didn't always work because when saving to the bag and then reading back, the domain model was not deserialized correctly.
Then I changed my VM implementation to be just a copy of the properties I needed from the DM:
public string Nome
{
get
{
return _nome;
}
set
{
if (value !=nome)
{
_nome= value;
NotifyPropertyChanged("Nome");
}
}
}
and with the constructor that does:
_nome = dm.Nome;
And now it works, but I was not sure if this is the right approach.
Thx
Simone
Any transient state information should be persisted in the Application.Deactivated event and then restored in the Application.Activated event for tombstoning support.
If you need to store anything between application sessions then you could use the Application.Closing event, but depending on what you need to store, you could just store it whenever it changes. Again, depending on what you need to store, you can either restore it in the Application.Launching event, or just read it when you need it.
The approach that you take depends entirely on your application's requirements and the method and location that you store your data is also up to you (binary serialization to isolated storage is generally accepted is being the fastest).
I don't know the details of your application, but saving and restoring data in NavigatedFrom/NavigatedTo is unlikely to be the right place to do it if you are looking to implement support for tombstoning.
I'd recommend against making a copy of part of the model as when tombstoning you'd (probably) need to persist both the full (app level) model and the page level copy when handling tombstoning.
Again the most appropriate solution will depend on the complexity of your application and the models it uses.
Application.Activated/Deactivated is a good place to handle tombstoning.
See why OnNavigatedTo/From may not be appropriate for your needs here.
How to correctly handle application deactivation and reactivation - Peter Torr's Blog
Execution Model Overview for Windows Phone