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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:56:25+00:00 2026-05-28T14:56:25+00:00

like the topic states – i’m trying to update a NSManagedObject in another thread.

  • 0

like the topic states – i’m trying to update a NSManagedObject in another thread.
my iPhone app is downloading multiple Asset and wants to update the downloadStatus of the given object. because of the two different threads i’m creating a new NSManagedObjectContext and fetch the Asset in the main method of my NSOperation / ASIHTTPRequest.

the first 1-n Assets are downloaded without problems but then i get an EXC_BAD_ACCESS while trying to save the context.

here is my code

AssetDownload.h

#import "ASIHTTPRequest.h"

@interface AssetDownload : ASIHTTPRequest
{
    @private
    Asset *_tempAsset;
    NSManagedObjectID *_assetId;
    NSManagedObjectContext *_ctx;
}

@property (nonatomic, strong) NSManagedObjectID *assetId;
@property (nonatomic, strong) NSManagedObjectContext *ctx;

- (id) initWithURL:(NSURL *)assetUrl andAsset:(NSManagedObjectID *)assetId;
+ (id) requestWithURL:(NSURL *)newURL andAsset:(NSManagedObjectID *)assetId;

@end

and the AssetDownload.m

#import "AssetDownload.h"
#import "DataController.h"

@interface AssetDownload (Private)
- (void) checkArticleStatusForAsset;
@end

@implementation AssetDownload

@synthesize assetId=_assetId;
@synthesize ctx=_ctx;

- (id) initWithURL:(NSURL *)assetUrl andAsset:(NSManagedObjectID *)assetId
{
    self = [self initWithURL:assetUrl];
    if (self)
    {
        self.assetId = assetId;
    }

    return self;
}

//
- (void) main
{   
    // CORE DATA & MULTITHREADING 
    _ctx = [[NSManagedObjectContext alloc] init];
    [self.ctx setUndoManager:nil];
    [self.ctx setPersistentStoreCoordinator: [[DataController sharedInstance] persistentStoreCoordinator]];

    // Register context with the notification center
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 
    [nc addObserver:self
           selector:@selector(mergeChanges:) 
               name:NSManagedObjectContextDidSaveNotification
             object:self.ctx];

    // get the asset from temp managedContext
    NSError *err;
    _tempAsset = (Asset *) [self.ctx existingObjectWithID:self.assetId error:&err];
    if (_tempAsset == nil) 
    {
        // asset not found
        NSLog(@"download asset data not found in CoreData - cancel download");
        return;
    }

    if ([_tempAsset isAvailable]) 
    {
        NSLog(@"AssetDownload main() >>> already downloaded -> COMPLETE");
        complete = YES;
        [self markAsFinished];
        [self checkArticleStatusForAsset];
        return;
    }

    NSLog(@"AssetDownload main() >>> download");    
    [super main];
}

//
- (void) requestFinished
{
    NSLog(@"AssetDownload requestFinished() >>> %i", self.responseStatusCode);

    NSError *mError;
    NSFileManager *fmngr = [NSFileManager defaultManager];
    if (self.responseStatusCode == 200)
    {
        if ([fmngr moveItemAtPath:self.downloadDestinationPath toPath:_tempAsset.localPath error:&mError])
        {
            NSLog(@"file moved: %@", _tempAsset.localPath);
            _tempAsset.downloadStatus = DownloadStatusComplete;
            [self checkArticleStatusForAsset];
        }
        else
        {
            NSLog(@"ERROR file not moved: %@ ... %@", _tempAsset.localPath, mError);
            _tempAsset.downloadStatus = DownloadStatusError;
        }
    }
    else
    {
        [fmngr removeItemAtPath:self.downloadDestinationPath error:nil];
        _tempAsset.downloadStatus = DownloadStatusError;
    }

    NSError *sError;
    [self.ctx save:&sError];

    [super requestFinished];
}


//
- (void) failWithError:(NSError *)theError
{
    NSLog(@"AssetDownload failWithError() >>> %@", theError);
    _tempAsset.downloadStatus = DownloadStatusError;

    [self.ctx save:nil];

    [super failWithError:theError];
}


//
- (void) checkArticleStatusForAsset
{
    if (_tempAsset.article.isLoaded)
    {
        NSDictionary *info = [NSDictionary dictionaryWithObject:_tempAsset.article forKey:@"article"];
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationArticleAssetsLoaded 
                                                            object:self
                                                          userInfo:info];
    }
}


#pragma mark -

- (void) mergeChanges:(NSNotification *)notification
{
    if ([notification object] == self.ctx)
    {
        NSLog(@"MERGE !");

        NSManagedObjectContext *mainContext = [[DataController sharedInstance] managedObjectContext];

        // Merge changes into the main context on the main thread
        [mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) 
                                      withObject:notification
                                   waitUntilDone:YES];  
    }
}


#pragma mark -


//
+ (id) requestWithURL:(NSURL *)newURL andAsset:(NSManagedObjectID *)assetId andManager:(AssetManager*)manager
{
    return [[self alloc] initWithURL:newURL andAsset:assetId andManager:manager];
}

@end

and this is where the ERROR happens (in the requestFinished:)

NSError *sError;
[self.ctx save:&sError];

maybe someone can explain to me why this is happening!?

  • 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-28T14:56:26+00:00Added an answer on May 28, 2026 at 2:56 pm

    ok, i refactored the whole thing and moved the update to the Asset class.

    instead of fetching the Asset with its ObjectID i just pass it into the AssetDownload class and do a

    - (void) updateDownloadStatus:(TCRDownloadStatus)status
    {
        NSNumber *statusNum = [NSNumber numberWithInt:status];
        [self.asset performSelectorOnMainThread:@selector(updateDownloadStatus:)    
                                     withObject:statusNum
                                  waitUntilDone:YES];
    }
    

    whenever the status changes.

    works great so far … and is far less code than before 🙂

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

Sidebar

Related Questions

The topic generically says it all. Basically in a situation like this: boost::scoped_array<int> p(new
I've ignored this topic for really long time and I would like to get
i have various strings that look like that: $(gateway.jms.jndi.ic.url,0,tibjmsnaming, tcp)/topic/$(gateway.destination.prefix)$(gateway.StatusTopicName),$(gateway.jms.jndi.ic.username),$(gateway.jms.jndi.ic.password),abinding,tBinding i'm trying to figure
Like most people new to Git, I've had my share of confusion trying to
As the topic states, sometimes these issues conflict. For example... In this case, the
I'm trying to route a path like this: http://www.wikipediamaze.com/wiki/Washington,_D.C. The routing framework is not
I've looked around on SO on this topic like this topic here . Also
$sql = sprintf( SELECT topic_title FROM `phpbb_topics` WHERE `topic_title` LIKE '%%%s%%' LIMIT 20 ,
Like many others on this site I am considering a move to ASP.NET MVC
Like the Delicious submission bookmark-let, I'd like to have some standard JavaScript I can

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.