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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:30:07+00:00 2026-05-26T15:30:07+00:00

I am trying to refactor my code so that it follows good OO pattern,

  • 0

I am trying to refactor my code so that it follows good OO pattern, which is reusability and less repetition.. I have the following code:

 PFQuery * query = [PFQuery queryWithClassName:@"Vote"];
    query.cachePolicy = kPFCachePolicyNetworkElseCache;
    [query whereKey:@"poll" equalTo:self.vote];
    [query whereKey:@"note" notEqualTo:nil];
    [query countObjectsInBackgroundWithBlock:^(int number, NSError * error) {
        note = number;
        [self.noteCount addSubview:self.generateCountLabel];

        if (note > 0){
            [self.note setUserInteractionEnabled:YES];
            [self.note setEnabled:YES];
        }
    }];

    PFQuery * query1 = [PFQuery queryWithClassName:@"Vote"];
    query1.cachePolicy = kPFCachePolicyNetworkElseCache;
    [query1 whereKey:@"poll" equalTo:self.vote];
    [query1 whereKey:@"image" notEqualTo:nil];
    [query countObjectsInBackgroundWithBlock:^(int number, NSError * error) {
        pic = number;    
        [self.picCount addSubview:self.generateCountLabel];

        if (pic > 0){
            [self.pic setUserInteractionEnabled:YES];
            [self.pic setEnabled:YES];
        }

    }];

Found it really difficult to partition into one method without doing a lot of if’s. Any idea?

  • 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-26T15:30:07+00:00Added an answer on May 26, 2026 at 3:30 pm

    You can have the following method….

    - (void)queryForKey:(NSString *)aKey view:(UIView *)aView countView:(UIView *)aCountView  {
        PFQuery * query = [PFQuery queryWithClassName:@"Vote"];        
        query.cachePolicy = kPFCachePolicyNetworkElseCache;
        [query whereKey:@"poll" equalTo:self.vote];
        [query whereKey:aKey notEqualTo:nil];
        [query countObjectsInBackgroundWithBlock:^(int number, NSError * error) {
            count = number;    
            [aCountView addSubview:self.generateCountLabel];
    
            if (count > 0){
                [aView setUserInteractionEnabled:YES];
                [aView setEnabled:YES];
            }
        }];
    }
    

    Then you can call…

    [self queryForKey:@"note" view:self.note countView:self.noteCount];
    [self queryForKey:@"image" view:self.pic countView:self.picCount];
    

    If you firm up your naming you can use dynamic object selectors, i.e. for the ‘note’ one you have @”note”, self.note and self.noteCount so you could take the @”note” and select both the note and noteCount views dynamically (as they both start with ‘note’), however this wouldn’t work for your image one as you have @”image” but then self.pic and self.picCount.

    UPDATE

    Rename image to pic and then expose the four views (picCount, pic, note and noteCount) as properties. Then you can use something like this…

    - (void)queryForKey:(NSString *)aKey  {
        PFQuery * query = [PFQuery queryWithClassName:@"Vote"];        
        query.cachePolicy = kPFCachePolicyNetworkElseCache;
        [query whereKey:@"poll" equalTo:self.vote];
        [query whereKey:aKey notEqualTo:nil];
        [query countObjectsInBackgroundWithBlock:^(int number, NSError * error, NSString *aKey) {
            [self setValue:number forKey:aKey];
            UIView *countView = [self objectForKey:[NSString stringWithFormat:@"%@Count", aKey]];
            [countView addSubview:self.generateCountLabel];
    
            if (number > 0){
                UIView *mainView = [self objectForKey:aKey];
                [mainView setUserInteractionEnabled:YES];
                [mainView setEnabled:YES];
            }
        }];
    }
    

    You will have to play around with it as I don’t have your full class definition to get it exactly right.

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

Sidebar

Related Questions

I have the following code for adding to/extracting from Zip. I'm trying to refactor
I have repetitive code that i am trying to refactor into a generic function
I have inherited some verbose, repetitious code that I am trying to refactor. The
I have some mutable scala code that I am trying to rewrite in a
I am trying attempting to refactor the following code: public static void SaveSplitbar(RadSplitBar splitBar)
I'm trying to refactor some code here that was done previously by other guys,
I'm trying to refactor this code. I have this jquery ajax call $.ajax({ url:
Im currently trying to refactor a project(asp.net mvc) that doesnt have any separation at
So I have this one CakePHP web application that I'm trying to refactor. There
I'm trying to refactor some python code which I'm using for financial analytics processing

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.