Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8377635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:43:21+00:00 2026-06-09T15:43:21+00:00

I have a Game class that has a -(void) play method which will be

  • 0

I have a Game class that has a -(void) play method which will be executed when the user clicks on the Play button on the device.

Inside the -(void) play method I have a while loop that will be executed repeatedly until the user clicks on the Quit button. This while loop is basically the core of my code, where all necessary methods are being called, things happen, objects interact etc.

I also have a User class (amongst other classes..) and I create a User* player instance in the -(void) play method of my Game class to store some values and have those interact with other things along the duration of the game..

Now I need to know (at any moment during the game..) the device’s deviation from the magnetic North & the acceleration the user is exercising on the device

I’ve written the code and everything is working fine. However, being new to programming I have a few questions concerning the overall design of my code, which I think is a mess especially when it comes to using the CoreLocation & CoreMotion frameworks..

The -(void) play method of the Game class (which is basically my “main” method) is executed on a separate thread as in [game performSelectorInBackground:@selector(play) withObject:nil]; Is this the right way to do it?

However, I initialise CoreMotion Acceleration updates from inside the -(void) play method as in [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]withHandler:^(CMDeviceMotion *motion, NSError *error){...} which means that updates will be stored in the main Queue whereas the method initializing these updates is executed from inside a method(-(void) play) that runs on a separate thread. Does that make sense?

I also initialise CoreLocation updates when I initialize an instance of my Game class. Even more weird?

My point is this. Given that I’ll be measuring the acceleration the user is exercising on the device and the orientation he/she is giving to the device (degrees) I want to encapsulate all that in my User class and have methods like [player getMyDegrees]; and [player getMyAcceleration]; Isn’t this the correct way design-wise? Where exactly should I initialize those updates? From inside which specific class-method? Should everything be running on the same main thread or the same separate thread or on different separate threads? I’m confused..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:43:23+00:00Added an answer on June 9, 2026 at 3:43 pm

    Threading is a complex issue, as you’ve no doubt seen.

    The -(void) play method of the Game class (which is basically my
    “main” method) is executed on a separate thread as in [game
    performSelectorInBackground:@selector(play) withObject:nil]; Is this
    the right way to do it?

    It depends. Is there any work that you are doing in the -play method that absolutely must be taken off the main thread (i.e. crazy number crunching, server requests, loading textures, etc.) so as to make the UI more responsive? That’s really the only reason methods are dispatched to the background on iOS, is to maintain a responsive UI, because UIKit classes are not just thread-unsafe, but they will wholeheartedly dive straight off a cliff and take your app with them if you try to do anything in the background.

    However, I initialise CoreMotion Acceleration updates from inside the
    -(void) play method … which means… these updates are executed from inside a method(-(void) play) that runs on a separate thread. Does
    that make sense?

    Well, again it depends here. The ultimate goal of threading is not to have two or more threads attempt to access or mutate one object held by another thread, which is just the start of a whole mess of problems. The one thing that I have to take issue with is the fact that you are using the main queue despite a very clear and ominous warning in the documentation:

    Because the processed events might arrive at a high rate, using the
    main operation queue is not recommended.

    This is an example of one of those high powered calculations that you should be throwing to a background thread, which is NSOperationQueue‘s forte.

    I want to encapsulate all that in my User class and have methods like
    [player getMyDegrees]; and [player getMyAcceleration]; Isn’t this the
    correct way design-wise? Where exactly should I initialize those
    updates? From inside which specific class-method?

    It depends on how design-wise you’re trying to be, and what you consider good design. Good code is usually code that you could hand off to another programmer and have them pick up where you started off with very little effort. That being said, it sounds like your user class is going to be the controller part of MVC. If your classes are too bulky, cut ’em up. Classes in objective-c are surprisingly light-weight creatures. Even arranging one class in multiple categories that describe the general section’s implementation are fine (the header of NSString for example).

    Should everything be running on the same main thread or the same
    separate thread or on different separate threads? I’m confused..

    Everything UI and light should be running in the main thread (or main queue if that’s how you prefer to look at it), and everything heavy and thread-blocking should be put in a background thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my game I have a concept of resource manager, a class that stores
I am making a card game in ruby. I have the Game class, which
Ok. I have a class Game, which creates an instance of my class Board,
I have a class called Mouse (tracking button states in a game). I want
Okay, I'm writing a game that has a vector of a pairent class (enemy)
I have a class that represents an capture zone in a game. Inside of
I am making a game which has a global (static) class called MedGameController. In
In my game engine I have a State class that represents the game world
I have an game application that users play between devices. I have added a
So I am writing a class that plays the card game war. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.