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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:43:37+00:00 2026-06-17T01:43:37+00:00

I’ve got 2 classes, MPRequest and MPModel . The MPModel class has a method

  • 0

I’ve got 2 classes, MPRequest and MPModel.
The MPModel class has a method to lookup something from the core data store, and if not found, creates an MPRequest to retrieve it via a standard HTTP request (The method in MPModel is static and not and instance method).

What I want is to be able to get a progress of the current HTTP request. I know how to do this, but I’m getting a little stuck on how to inform the view controller. I tried creating a protocol, defining a delegate property in the MPRequest class, altering the method in MPModel to accept this delegate, and in turn passing it to the MPRequest when it is created.

This is fine, however ARC is then releasing this delegate whilst the request is running and thus doesn’t do what I want. I’m trying to avoid making my delegate object a strong reference in case it throws up any reference cycles but I don’t know any other way of doing this.

To start the request, from my view controller I’m running

[MPModel findAllWithBlock:^(NSFetchedResultsController *controller, NSError *error) {
    ....
} sortedBy:@"name" ascending:YES delegate:self]

Inside the findAllWithBlock method, I have

MPRequest *objRequest = [MPRequest requestWithURL:url];
  objRequest.delegate = delegate;

[objRequest setRequestMethod:@"GET"];
[MPUser signRequest:objRequest];

[objRequest submit:^(MPResponse *resp, NSError *err) {
    ...
}

And in the MPRequest class I have the following property defined :

@property (nonatomic, weak) NSObject<MPRequestDelegate> *delegate;

Any ideas or suggestions?

As requested, here is some more code on how things are being called :

In the view controller :

[MPPlace findAllWithBlock:^(NSFetchedResultsController *controller, NSError *error) {
        _placesController = controller;
        [_listView reloadData];
        [self addAnnotationsToMap];
        [_loadingView stopAnimating];

        if (_placesController.fetchedObjects.count > 0) {
            // We've got our places, but if they're local copies
            // only, new ones may have been added so just update
            // our copy
            MPSyncEngine *engine = [[MPSyncEngine alloc] initWithClass:[MPPlace class]];
                 engine.delegate = self;
            [engine isReadyToSync:YES];
            [[MPSyncManager sharedSyncManager] registerSyncEngine:engine];
            [[MPSyncManager sharedSyncManager] sync];
        }
    } sortedBy:@"name" ascending:YES delegate:self];

Here, self is never going to be released for obvious reasons, so I don’t see how this is the problem.

Above, MPPlace is a subclass of MPModel, but the implementation of the findAllWithBlock:sortedBy:ascending:delegate: is entirely in MPModel

The method within MPModel looks like this

NSManagedObjectContext *context = [[MPCoreDataManager sharedInstance] managedObjectContext];
[context performBlockAndWait:^{
    __block NSError *error;
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([self class])];
    [request setSortDescriptors:@[[[NSSortDescriptor alloc] initWithKey:key ascending:asc]]];

    NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                                 managedObjectContext:context
                                                                                   sectionNameKeyPath:nil
                                                                                            cacheName:nil];

    [controller performFetch:&error];

    if (!controller.fetchedObjects || controller.fetchedObjects.count == 0) {
        // Nothing found or an error, query the server instead
                NSString *url = [NSString stringWithFormat:@"%@%@", kMP_BASE_API_URL, [self baseURL]];
        MPRequest *objRequest = [MPRequest requestWithURL:url];
          objRequest.delegate = delegate;

        [objRequest setRequestMethod:@"GET"];
        [MPUser signRequest:objRequest];

        [objRequest submit:^(MPResponse *resp, NSError *err) {
            if (err) {
                block(nil, err);
            } else {
                NSArray *objects = [self createListWithResponse:resp];
                         objects = [MPModel saveAllLocally:objects forEntityName:NSStringFromClass([self class])];
                [controller performFetch:&error];
                block(controller, nil);
            }
        }];
    } else {
        // Great, we found something :)
        block (controller, nil);
    }
}];

The delegate is simply being passed on to the MPRequest object being created. My initial concern was that the MPRequest object being created was being released by ARC (which I guess it probably is) but it didn’t fix anything when I changed it. I can’t make it an iVar as the method is static.

The submit method of the request looks like this :

_completionBlock = block;
   _responseData = [[NSMutableData alloc] init];

[self prepareRequest];

[self prepareRequestHeaders];
_connection = [[NSURLConnection alloc] initWithRequest:_urlRequest
                                              delegate:self];

And when the app starts downloading data, it calls :

[_responseData appendData:data];
[_delegate requestDidReceive:(float)data.length ofTotal:_contentLength];

Where _contentLength is simply a long storing the expected size of the response.

  • 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-17T01:43:39+00:00Added an answer on June 17, 2026 at 1:43 am

    Got it working. It was partly an issue with threading, where the core data thread was ending before my request, me looking at the output from a different request entirely, and the way ARC handles memory in blocks.

    Thanks for the help guys

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am using jsonparser to parse data and images obtained from json response. When
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
In my XML file chapters tag has more chapter tag.i need to display chapters

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.