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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:11:47+00:00 2026-06-15T06:11:47+00:00

Okay so i have this program that is suposed to communicate over a Webserver(PHP

  • 0

Okay so i have this program that is suposed to communicate over a Webserver(PHP Script)

I have this method that i want to get a random id for a table. The return data of this NSURLconnection is supose to be the amount of rows in the table.

First time i call it, its like randomid variables does not get set, but second time i run it, it does get set.

variable setting:

@implementation ViewController {
NSMutableData *randomData;
int randomid;
}

Here is my IBaction for a button:

- (IBAction)initVits:(id)sender {
[self randomID];
NSLog(@"Random ID: %d", randomid);    
}

The output first time is: Random ID: 0, second time: Random ID: a valid id

RandomID:

- (void) randomID {

NSString *url = @"http://ivitserdk.porkhost.dk/ivitserdk.php?function=randomid";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]   cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:1.0];
connectionRandomID = [[NSURLConnection alloc] initWithRequest:request delegate:self];

randomData = [NSMutableData data];
[connectionRandomID start];
}

Connection did finish loading:

 if(connection == connectionRandomID) {
    NSString *String = [[NSString alloc] initWithData:randomData encoding:NSUTF8StringEncoding];
    NSLog(@"Return: %@", String);
    int temp = [String integerValue];
    NSLog(@"temp: %d", temp);
    randomid = (arc4random() % temp) + 1;
    NSLog(@"Random: %d", randomid);
    randomData = nil;
    connectionRandomID = nil;
 }

Output is for all times: Return: (number of rows in table)
temp: (number of rows in table to a int)
Random: (a random number)

Its like the randomid variable does not get set the first time.

Thanks for your time!

  • 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-15T06:11:48+00:00Added an answer on June 15, 2026 at 6:11 am

    You are using asynchronous networking, so your NSLog is called before the random ID is retrieved from the server. There are multiple solutions to this.

    • Use synchronous networking (I don’t recommend this, so I don’t go further into that)
    • Put the NSLog statement or whatever you want to do when the ID is generated at the end of your connectionDidFinishLoading method
    • If possible, I’d recommend generating the ID on the device (but I guess you need an unique key for a database, an URL or something, so this will probably not work for you)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I have this program and I don't want more than one instance of
Okay, I have this code: <script language=javascript type=text/javascript> <!-- function requestPage(page) { var request
Okay, so I have this form that is set to preload a DB record
okay i have a file that looks like this: 2 3 6 6 22
Okay this is what I have so far. I have a form that needs
Okay so I have a program that asks for a number of people. Then,
Okay, So I have a program that automatically copies data to the clipboard and
Okay, so I have this Silverlight client program. I'm not allowed to use the
I have this script that works ok. Trouble is that when I make Click
Okay so i have this code: if (isset($_GET['book'])) { $query= SELECT book_id, title, authors.`author`

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.