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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:21:49+00:00 2026-06-06T14:21:49+00:00

I have created a class for fetching server-side data in my iOS-App, based on

  • 0

I have created a class for fetching server-side data in my iOS-App, based on NSURLConnection, forwarding the fetched data to a delegate for processing.

When downloading text/json this works perfectly.

I am now expanding to fetch some png-images and can’t get the data through.

I have located that it breaks in the didReceiveData-call, where [receivedData appendData:data] do not do anything for the received image-data.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    [receivedData appendData:data];
}

I get data into this method (NSData >0 bytes), but calling [receivedData appendData:data] do not change the size of receivedData (Still 0 bytes).

As this works perfectly with text and not with image-data I believe it has something to do with characterset or encoding to do, but cannot find anything on this.

Any help is appreciated.


Update with requested code-samples:

In the method that makes the call:

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    // Create the NSMutableData to hold the received data.
    receivedData = [NSMutableData data];
    // Also tried with no change in behaviour
    // receivedData = [[NSMutableData alloc] initWithLength:0];
} else {
    // Inform the user that the connection failed.
}

My didReceiveResponse:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

Another UPDATE

Turns out the problem bois down to concurrency as I fire three instances of the class simultaniously to fetch three files asynchroniously. The problem is that the first releases receivedData when finishing ruining the response from the two other instances.

How do I make sure that each instance of the class has their own receivedData to play with?


Final Notes
Using a dictionary of MutableArrays for the retreived data solves the concurrency-problem.

It also seems like defining the MutableArray as a private property of the class creates one array for each instance.

@interface MyClass(){
    @private NSMutableData * receivedData;
}
  • 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-06T14:21:51+00:00Added an answer on June 6, 2026 at 2:21 pm

    Do you ever initialize receivedData?

    Something like this would do it:

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        if(!receivedData)
        {
            receivedData = [NSMutableData data];
        }
        [receivedData appendData:data];
    }
    

    An even faster approach is to initialize it right before you kick off the actual request (so that it’s not being checked every time data is received).

    also remember to nil receivedData when your request is completed and you are done extracting it so that it will be ready to go for the next request.


    Here’s one solution for concurrent requests:

    Instead of a single NSMutableData property, create an NSMutableDictionary so that you can hang on to multiple instances at once.

    NSMutableDictionary *receivedData

    Now, once you put together your NSURLConnection (let’s call it urlConnection), you can create a new unique data object and put it in your dictionary. You should be able to use the string description of the connection as your unique key.

    if(!receivedData)
      receivedData = [NSMutableDictionary dictionary];
    
    NSMutableData *dataObject = [NSMutableData data];
    [receivedData setObject:dataObject forKey:[urlConnection description]];
    

    Now you can do this:

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
      NSMutableData *currentData = (NSMutableData*)[receivedData objectForKey:[connection description]];
      [currentData.appendData:data];
    }
    

    Once your connection has completed, remember to call removeObjectForKey to get rid of data objects once they are no longer needed.

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

Sidebar

Related Questions

I have created a Class Library (Core Processing Component) using C# in Visual Studio
I have created a service class for my network connection so that my app
I have created new class to read the data from xml file, which looks
I have a class A which is responsible for fetching data from web services
I have created a class library called AddServiceLibrary in which I have a method
I have created a class named Times and I have to construct 4 overloaded
I have created a class that will manage connection and commands, I named it
I have created a class as follows: #include <iostream> #define DLLEXPORT _declspec( dllexport )
I have created a class library as a project. Where I have added a
I am using C#. I have created a class which can be included in

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.