• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - JSON 调用运行时暂停功能

[复制链接]
菜鸟教程小白 发表于 2022-12-12 22:24:29 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我希望我的保存按钮运行 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 "rofileManager.h"
#import "rofileCommunicator.h"
#import "SteamProfile.h"
#import "DeviceViewController.h"

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

@end

@implementation DeviceDetailViewController

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

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

- (IBAction)saveid)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)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)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)didReceieveProfileInfoNSArray *)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)fetchingGroupsFailedWithErrorNSError *)error
{
    NSLog(@"Error %@; %@", error, [error localizedDescription]);
}

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


@end



Best Answer-推荐答案


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

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

- (void)startFetchingGroupsvoid(^)(int))handler;
@end



@implementation MyClass


- (void)startFetchingGroupsvoid(^)(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 的实际作用。您也可以像评论中建议的一些人一样使用委托(delegate),这绝对是一个干净的解决方案。

关于ios - JSON 调用运行时暂停功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23850658/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap