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

  • SEARCH
  • Home
  • 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 7609631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:13:08+00:00 2026-05-31T01:13:08+00:00

In short, I would like to, in Objective-C cocoa, program something that functions the

  • 0

In short, I would like to, in Objective-C cocoa, program something that functions the same way as the following Java pseudocode:

public class MainClass
{
    public void mainmethod() //Gets called at start of program
    {
        UILabel label = CreateAButton();
        new DaemonClass(label).start();
        //Do things without being interrupted by the Daemon class sleeping or lagging
    }
}

public class DaemonClass extends Thread
{
    public UILabel label;
    public DaemonClass(UILabel lbl)
    {
        setDaemon(true);
        label = lbl;
    }

    public void run()
    {
        int i = 0;
        while(true)
        {
            i++;
            i = i%2;
            UILabel.setText("" + i);
            Thread.sleep(1000);
        }
    }
}

In other words… I’d like to spawn a daemon thread that can be as slow as it likes, without interrupting the progress or speed of any other threads, INCLUDING, the main one.

I have tried using things like the Dispatch Queue, as well as NSThread.

When using either of these, I tried to create a simple label-changer thread that toggled the label’s text from 1 to 0, indefinitely. It appeared to me, the user, to constantly be locked either at 1, or 0, randomly chosen at startup.

When using either of these, and attempting to use [NSThread sleepForTimeInterval:1];, the thread would stop executing all together after the sleepForTimeInterval call.

Furthermore, having skimmed the docs, I picked up on the fact that the run loop is not called while [NSThread sleep... is sleeping!

If it is any help, I was invoking my threads from the - (void)viewDidLoad; method.

My question for you is:

How do I stop [NSThread sleepForTimeInterval:1]; from crashing my thread, OR:
How do I start a daemon thread that invokes a method or code block (preferably a code block!)

P.S. if it makes any difference, this is for iOS

  • 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-05-31T01:13:09+00:00Added an answer on May 31, 2026 at 1:13 am

    The reason for the problems you’ve seen is most likely that UIKit isn’t thread-safe, i.e. you can only use a UILabel from the main thread. The easiest way to do that is to enqueue a block on the main queue (which is associated with the main thread) using GCD:

    dispatch_async(dispatch_get_main_queue(), ^{
        myLabel.text = @"whatever";
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement operations that is undoable in a short time after
I have a native objective c ios application that I would like to convert
I would like to generate a short, unique ID without having to check for
I would like to determine what the long url of a short url is.
I'm working on an Objective-C/Cocoa roguelike, because I've always found that working on a
I would like to make a queue wait for a short period while it
I'm in the process of learning Objective-c but would like to run code snippets
I would like to use Short Date named string format in WPF. I tried
I am a beginner in this very confusing Objective-C game. I would like to
How can I store an Objective-C++ short int like the one below in an

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.