The platform is monotouch
I develop an app for iphone.
I have a class called Globals. The "Globals" object collects the entries from UI and the calculations during the game.
I plan to serialize the object and stream it to a txt file . And recover it when the game launched again. ( Remember I create the serialized file only the program terminated by the user)
I use the deserialized object to go on from the moment of the user has terminated the program.
Is this way okay? Is it a best practise? or I need to use other ways to keep the game's info all the time. Please comment or advice a best practise for this purpose.
If you go the SQLite route look at SQLite.Net it will save your exiting objects and load them with hardly any code.
https://github.com/praeclarum/sqlite-net#readme
here is an example:
// Add some extra atributes as needed to your existing class.
public class Stock
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(8)]
public string Symbol { get; set; }
}
Create a SQLite db connection and make sure the table for the class Stock exists.
var db = new SQLiteConnection("foofoo");
db.CreateTable<Stock>();
Add a Stock instance to the DB... eg.. Save it!
public static void AddStock(SQLiteConnection db, string symbol) {
var s = db.Insert(new Stock() {
Symbol = symbol
});
Console.WriteLine("{0} == {1}", s.Symbol, s.Id);
}
Get one or more Stock(s) back from DB....
public static IEnumerable<Stock> QueryValuations (SQLiteConnection db, int stock)
{
return db.Query<Stock> ("select * from Stock where StockId = ?", Id);
}
You should really try to use CoreData and make an Entity out of your Globals object. Using Core Data will open you up to more features (like iCloud sync for example) but also you can update it as the object changes. If you don't and only save when the app terminates, then you take the risk of loosing the user's changes for example if there is an abnormal termination (like a crash for example).
Take a look at Core Data. There are plenty of easy tutorials online. Here is one: http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started
Serializing and deserializing might be a bit slow according to the size of data as whole data is fetched at a time.This could work for small data but the native methids such as coredata,plist,sqlite or NSUserDefaults would be more acceptable.CoreData and sqlite can be used for even large data.
Sqlite Tutorial
Core Data Tutorial
NSUserdefaults Tutorial