you should pick simple concepts as the ones you tackle in this way. We think in this respect, though, that because things can be deadends in I.T. We think this approach of taking a simple concept for a web application and rewriting it in different ways is instructive, and you learn quite a bit doing this, we think. Yesterday’s Dynamic Javascript and the YouTube Embedded API Tutorial got us thinking about dynamic Javascript, and today we put that concept through its paces by writing another incarnation of the Tic Tac Toe (or Noughts and Crosses) game we’ve written a few versions of now. (void)applicationWillResignActive:(UIApplication *)applicationĪt this point we have a fully functioning TicTacToe game, we have successfully integrated the Simperium Framework, and we’re automatically authenticating the user when the app is brought to foreground (and logging off when the app is closed).Dynamic Javascript Tic Tac Toe Game Tutorial (void)applicationDidBecomeActive:(UIApplication *)application We’ll rely on the following lines of code to do so: We must also call the method authenticateSimperiumIfNeeded everytime the app becomes active, and signout from Simperium’s backend when the app becomes inactive. Īfter calling the two routines outlined above, we’ll have a Simperium instance initialized with manual authentication, and our CoreData stack will be ready to begin operating. Self.simperium = initWithModel:manager.managedObjectModelĬoordinator:manager.persistentStoreCoordinator] We’ll be initializing and authenticating our Simperium instance in the AppDelegate itself, as soon as the application:didFinishLaunchingWithOptions: method is executed: persistentStoreCoordinator must be nil on the main context!: The only difference with a regular CoreData stack initialization can be seen below: _managedObjectContext = initWithConcurrencyType:NSMainQueueConcurrencyType] In our TicTacToe game, we’ve wrapped up all the CoreData initialization code in a single class, called TTCoreDataManager ( Header | Implementation). ![]() As a requirement for this to work, the Main ManagedObjectContext must not have its persistentStoreCoordinator set. One of the benefits of using Simperium’s library is that Core Data disk operations are performed in a background thread, automatically, by the library. You can find the Generate Token button in the Browse Data section of your app: In order to do so, let’s pre-generate a user token: every instance of this game will share the same user. However, in this particular project, we want to offer a signup-free experience: we want our users to be able to begin playing Tic Tac Toe, as soon as the app finishes launching. There are several ways in which you can implement user authentication: the framework itself already provides a customizable User Interface to perform Signup and Sign-In. We’ll need both, the AppId and AppKey, to initialize our instance of Simperium: We’ll need to signup for an account at Simperium’s website. Let’s open our Data Model, and update both, Match and Player entities, accordingly: We’ll need to also store two extra attributes per object, used internally by Simperium: simperiumKey and ghostData, both of NSString kind. Simperium is designed to listen to your CoreData Context, and efficiently sync delta’s through the network.Īs a requirement, the classes you want to sync must subclass SPManagedObject -instead of NSManagedObject. Integrating the Simperium Frameworkįirst of all, we’ll need to add drag the Simperium.xcproj file to our TicTacToe project, as seen below: The match object itself will have three attributes: the ID of both players, an array representing the status of the gameboard, and an extra flag we’ll use to indicate which player should perform the next move. ![]() We will maintain two collections:Įach player will have a unique ID, and will make sure that as long as the app is active, its uniqueID will be present in the Players collection. We will rely on this mechanism to initiate a match between two players. ![]() Our approach to implement the data model will be straight forward. We have built a single-device Tic Tac Toe game for iOS: source code available here. In this tutorial we’ll be learning how to integrate the Simperium framework, and how to enable multi-device data synchronization without writing a single line of backend code. The first player that succeeds in placing three marks in either a horizontal, vertical, diagonal or antidiagonal wins the game. Tic Tac Toe is one of the simplest games you can possibly think of: two players take turns to place a piece (either a cross or a circle) on a 3×3 matrix. Simperium is a cross platform framework that allows you to seamlessly sync data through multiple devices.
0 Comments
Leave a Reply. |