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 8361311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:40:08+00:00 2026-06-09T11:40:08+00:00

I have a simple class with two ivars, a NSMutableArray and a BOOL .

  • 0

I have a simple class with two ivars, a NSMutableArray and a BOOL. Objects of this class are able to shuffle elements in the array when they are sent the startShuffling message. They do so until they receive the stopShuffling message.

To make it work, the startShuffling method set the boolean to YES, and then dispatch the block of code that shuffles (while(self.isShuffling) { //... } on a concurrent queue. The stopShuffling set the boolean to NO, so that the shuffling process will terminate on the next loop turn.

Here is the interface :

@interface MyClass : NSObject <NSCoding> {
@private
    NSMutableArray *elements_;
    __block BOOL isShuffling_;
}

@property(readonly) BOOL isShuffling;

-(void)startShuffling;
-(void)stopShuffling;

@end

And the implementation :

@implementation MyClass

@synthesize isShuffling = isShuffling_;

-(void)startShuffling {
    if(self.isShuffling) {
        return;
    }

    isShuffling_ = YES;

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        while(isShuffling_) {
            // code that shuffles, one element by turn
            NSUInteger elementIndex = arc4random() % [elements_ count];
            id c = [[elements_ objectAtIndex:elementIndex] retain];
            [elements_ removeObjectAtIndex:elementIndex];
            [elements_ insertObject:c atIndex:[elements_ count]];
            [c release];
        }
    });
}

-(void)stopShuffling {
    isShuffling_ = NO;
}

@end

My class conforms to NSCoding protocol and I don’t want to abort encoding even if the object is shuffling. Instead I want my object to stop shuffling and then encode itself. So I have written this encoding method :

-(void)encodeWithCoder:(NSCoder *)aCoder {
    if(self.isShuffling) {
        [self stopShuffling];
    }
    [aCoder encodeObject:elements_ forKey:kIVPCodingKeyMyClassElements];
}

Finally, here my question.
I think it is possible for the encodeObject:forKey: method to get called while the shuffling loop terminates its last turn (maybe I’m wrong ?).

Is there any way I can make encodeObject:forKey: method get called after wait for the shuffling loop last turn to terminate ?

  • 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-09T11:40:10+00:00Added an answer on June 9, 2026 at 11:40 am

    Yes, the the shuffle code may still be running when the encodeObject:forKey: method is called.

    In general, you don’t want to dispatch some random block off onto a queue that executes for a really long time, potentially forever. You want to break up the work into blocks of work. And there-in lies your answer.

    Something like:

     - (void)shuffleAndCheck
     {
        if (stillShuffling) {
             dispatch_async(globalConcurrentQueue, ^{
                  dispatch_apply(shuffleQueue, ^{... shuffle one card code ...});
             });
             dispatch_async(shuffleQueue, ^{ [self shuffleAndCheck]; });
        }
     }
    
     - (void) startShuffling
     {
        if (stillShuffling) return;
        stillShuffling = YES;
        [self shuffleAndCheck];
     }
    
     - (void) stopShuffling
     {
        stillShuffling = NO;
        dispatch_async(shuffleQueue, ^{ ... encode stuff here ... });
     }
    

    Or something.

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

Sidebar

Related Questions

I have a simple Fluent NHibernate model with two related classes: public class Applicant
I have these two simple files that define a C++ class with a tryME
I have a very simple scenario, using NHibernate: one abstract base class animal; two
if I have two simple models: class Tag(models.Model): name = models.CharField(max_length=100) class Post(models.Model): title
I have following two simple POJOs: class Person { String name Address address; //and
I have a really simple class with two methods; One that will be called
Suppose I have a simple Java class like this: public class User { String
I have a very simple class with two fields, String sourceAddress and int port.
I have simple controller with two actions: public class TestController : Controller { //
I have two IList<Traffic> I need to combine. Traffic is a simple class: class

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.