JSON调用正在运行时暂停功能

时间:2014-05-24 23:36:26

标签: ios json delay steam-web-api

我希望我的保存按钮能够运行JSON调用,然后完成它的功能,但它在运行JSON调用时完成了该功能,因此变量初始化为nil

我的DeviceDetailViewController.m

//
//  DeviceDetailViewController.m
//  Steam Backpack Viewer
//
//  Created by Vishwa Iyer on 5/22/14.
//  Copyright (c) 2014 MoAppsCo. All rights reserved.
//

#import "DeviceDetailViewController.h"
#import "MasterViewController.h"
#import "ProfileManager.h"
#import "ProfileCommunicator.h"
#import "SteamProfile.h"
#import "DeviceViewController.h"

@interface DeviceDetailViewController () <ProfileManagerDelegate> {
    ProfileManager *_manager;
    NSArray *profile;
    SteamProfile *s;
}
extern NSString *ID;

@end

@implementation DeviceDetailViewController

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)save:(id)sender {
    NSManagedObjectContext *context = [self managedObjectContext];

    // Create a new managed object
    NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"BackpackViewer" inManagedObjectContext:context];
    [newDevice setValue:self.steamIDTextField.text forKey:@"steamID"];
    ID = [NSString stringWithFormat:@"%@", [newDevice valueForKey:@"steamID"]];


    [self startFetchingGroups]; // I would like this JSON call to finish before calling the rest of the function below


    [newDevice setValue:s.personaname forKey:@"steamName"];
    [newDevice setValue:s.avatar forKey:@"imageURL"];


    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
    [self dismissViewControllerAnimated:YES completion:nil];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _manager = [[ProfileManager alloc] init];
    _manager.communicator = [[ProfileCommunicator alloc] init];
    _manager.communicator.delegate = _manager;
    _manager.delegate = self;
    // Do any additional setup after loading the view.
}

- (void)startFetchingGroups
{
    [_manager fetchGroups];
}

- (void)didReceieveProfileInfo:(NSArray *)groups
{
    //the JSON call finishes here, when the groups are receives from the call. I would then like the rest of the save button method above to run after this runs, so that the s variable (which corresponds to a SteamProfile object) becomes initialized correctly.
    profile = groups;
    s = [profile objectAtIndex:0];
    NSLog(s.personaname);

}

- (void)fetchingGroupsFailedWithError:(NSError *)error
{
    NSLog(@"Error %@; %@", error, [error localizedDescription]);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

2 个答案:

答案 0 :(得分:1)

我认为你在找这样的东西。请注意,这种语法可能是错误的,未经测试。我将留给您阅读有关函数回调的文档。

@interface MyClass: NSObject
{
    void (^_completionHandler)(int someParameter);
}

- (void)startFetchingGroups:(void(^)(int))handler;
@end



@implementation MyClass


- (void)startFetchingGroups:(void(^)(void))handler
{
    [_manager fetchGroups];
    if (handler) {
        handler();
    }
}

@end

[var startFetchingGroups:^{
     [newDevice setValue:s.personaname forKey:@"steamName"];
    [newDevice setValue:s.avatar forKey:@"imageURL"];


    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}];

调用回调时的行为取决于_manager fetchGroups实际执行的操作。你也可以在建议的评论中使用委托作为一些人,并且绝对是一个干净的解决方案。

答案 1 :(得分:0)

抱歉丑陋的格式化。这段代码正是你想要的......

#import "DeviceDetailViewController.h"
#import "MasterViewController.h"
#import "ProfileManager.h"
#import "ProfileCommunicator.h"
#import "SteamProfile.h"
#import "DeviceViewController.h"

typedef void(^EmptyBlock_t)();

@interface DeviceDetailViewController () <ProfileManagerDelegate> {
    ProfileManager *_manager;
    NSArray *profile;
    SteamProfile *s;

    //  1   
    //  here you define a block, an anonymous function pointer that will be called right after you callback is called..
    EmptyBlock_t _blockAfterJSONFetched;  
}
extern NSString *ID;

@end

@implementation DeviceDetailViewController

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)save:(id)sender {
    NSManagedObjectContext *context = [self managedObjectContext];

    // Create a new managed object
    NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"BackpackViewer" inManagedObjectContext:context];
    [newDevice setValue:self.steamIDTextField.text forKey:@"steamID"];
    ID = [NSString stringWithFormat:@"%@", [newDevice valueForKey:@"steamID"]];


    [self startFetchingGroups]; // I would like this JSON call to finish before calling the rest of the function below

    //  2
    //  here you assign a value to your block. Notice that all objects inside block are called "retain" automatically. Also they a called "release" when you release the block itself..
    _blockAfterJSONFetched=^{
        [newDevice setValue:s.personaname forKey:@"steamName"];
        [newDevice setValue:s.avatar forKey:@"imageURL"];


        NSError *error = nil;
        // Save the object to persistent store
        if (![context save:&error]) {
            NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    };
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _manager = [[ProfileManager alloc] init];
    _manager.communicator = [[ProfileCommunicator alloc] init];
    _manager.communicator.delegate = _manager;
    _manager.delegate = self;
    // Do any additional setup after loading the view.
}

- (void)startFetchingGroups
{
    [_manager fetchGroups];
}

- (void)didReceieveProfileInfo:(NSArray *)groups
{
    //the JSON call finishes here, when the groups are receives from the call. I would then like the rest of the save button method above to run after this runs, so that the s variable (which corresponds to a SteamProfile object) becomes initialized correctly.
    profile = groups;
    s = [profile objectAtIndex:0];
    NSLog(s.personaname);

    //  3
    //  finally after your callback is fired you check if block pointer is not null and if it is you call it as a casual function. Assigning nil in the end is optional..
    if(_blockAfterJSONFetched){
        _blockAfterJSONFetched();
        _blockAfterJSONFetched=nil;
    }

}

- (void)fetchingGroupsFailedWithError:(NSError *)error
{
    NSLog(@"Error %@; %@", error, [error localizedDescription]);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
相关问题