菜鸟教程小白 发表于 2022-12-12 22:24:29

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


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

<p>我的<code>DeviceDetailViewController.m</code></p>

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

#import &#34;DeviceDetailViewController.h&#34;
#import &#34;MasterViewController.h&#34;
#import &#34;ProfileManager.h&#34;
#import &#34;ProfileCommunicator.h&#34;
#import &#34;SteamProfile.h&#34;
#import &#34;DeviceViewController.h&#34;

@interface DeviceDetailViewController () &lt;ProfileManagerDelegate&gt; {
    ProfileManager *_manager;
    NSArray *profile;
    SteamProfile *s;
}
extern NSString *ID;

@end

@implementation DeviceDetailViewController

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

- (IBAction)cancel:(id)sender {
    ;
}

- (IBAction)save:(id)sender {
    NSManagedObjectContext *context = ;

    // Create a new managed object
    NSManagedObject *newDevice = ;
    ;
    ID = ];


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


    ;
    ;


    NSError *error = nil;
    // Save the object to persistent store
    if (!) {
      NSLog(@&#34;Can&#39;t Save! %@ %@&#34;, error, );
    }
    ;

}

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

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

- (void)startFetchingGroups
{
    ;
}

- (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 = ;
    NSLog(s.personaname);

}

- (void)fetchingGroupsFailedWithError:(NSError *)error
{
    NSLog(@&#34;Error %@; %@&#34;, error, );
}

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


@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为您正在寻找这样的东西。请注意,此语法可能是错误的,未经测试。我将留给您阅读有关函数回调的文档。 </p>

<pre><code>@interface MyClass: NSObject
{
    void (^_completionHandler)(int someParameter);
}

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



@implementation MyClass


- (void)startFetchingGroups:(void(^)(void))handler
{
    ;
    if (handler) {
      handler();
    }
}

@end

[var startFetchingGroups:^{
   ;
    ;


    NSError *error = nil;
    // Save the object to persistent store
    if (!) {
      NSLog(@&#34;Can&#39;t Save! %@ %@&#34;, error, );
    }
    ;
}];
</code></pre>

<p>回调被调用时的行为取决于 _manager fetchGroups 的实际作用。您也可以像评论中建议的一些人一样使用委托(delegate),这绝对是一个干净的解决方案。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - JSON 调用运行时暂停功能,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23850658/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23850658/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - JSON 调用运行时暂停功能