Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 是否使用领域数据填充UITableView(表)?_Objective C_Swift_Multithreading_Uitableview_Realm - Fatal编程技术网

Objective c 是否使用领域数据填充UITableView(表)?

Objective c 是否使用领域数据填充UITableView(表)?,objective-c,swift,multithreading,uitableview,realm,Objective C,Swift,Multithreading,Uitableview,Realm,我的解决方案不起作用-我使用realm获取数据并将其存储为数组,然后在cellforrowatinexpath:中重用此数组 问题是我应该在后台线程中获取数据,但在主线程中填充表 注意:我已经读到,如果我在当前线程中获得领域数据,那么我只能在这个线程中使用它们。否则,我应该为我需要的每个领域对象创建ThreadSafeReference,并在主线程中重用它。但是我不明白怎么做-这个例子是这样写的,他们在dispatch\u async之前创建一个对象,并在这个对象中使用它(比如将变量传递到代码块

我的解决方案不起作用-我使用realm获取数据并将其存储为数组,然后在
cellforrowatinexpath:
中重用此数组

问题是我应该在后台线程中获取数据,但在主线程中填充表

注意:我已经读到,如果我在当前线程中获得领域数据,那么我只能在这个线程中使用它们。否则,我应该为我需要的每个领域对象创建
ThreadSafeReference
,并在主线程中重用它。但是我不明白怎么做-这个例子是这样写的,他们在
dispatch\u async
之前创建一个对象,并在这个对象中使用它(比如将变量传递到代码块)

在我的例子中,我有一个单独的数组,我应该在
cellforrowatinexpath
中存储和重用它(它不是代码块),并多次调用它。我也不能调用
dispatch\u async
,因为单元格可能会变得无效


如何解决这个问题?我应该手动缓存领域对象吗?我接受Swift和Objective-C语言

以下代码适用于您:

// ViewController.h
#import <UIKit/UIKit.h>
#import "MyObject.h" // subclass of RLMObject

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property RLMResults<MyObject *> *objectsList;

@end
不需要从使用Dispatch的其他线程调用领域对象。唯一会出现这些“错误线程”错误的情况是,将视图控制器的引用传递给其他类,如后台网络客户端或类似的类


无论如何,这些情况也是可以避免的。如果您的对象获得了一个ID,比如说从一个API,那么您可以将该ID传递给另一个类,可能是后台同步客户端,然后从该客户端对该对象执行查询,如
[MyObject objectsWhere:@“objectID==%”,idString]。该对象是从将使用它的后台进程调用的,因此不会失败。

无需从使用Dispatch的其他线程调用域对象。
我希望您理解域作为数据库不是超高速的。因此,在更好的情况下,在显示视图控制器之前会有延迟,在最坏的情况下,会出现警告/错误的不可预测行为,因为您会减慢
viewdiload
methodRealm的速度,正如此答案所述。如果您实际有一个项目正经历这种减速行为,请随时向我们的GitHub()提交一份罚单,我们可以修复它所揭示的任何错误,或者帮助您找出错误所在。
// ViewController.m
#import "MyViewController.h"

@implementation MyViewController {

- (void)viewDidLoad {
    _objectsList = [MyObject allObjects]; // optionally sort, query, etc.
}

- (void)viewDidAppear {
    [super viewDidAppear];
    // Do any other setup
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _objectsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Setup the cell with the object's information. It's up to you how 
    // to do it. Register a custom subclass of UITableViewCell, for example.
    MyObject *thisCellObject = [objectsList objectAtIndex:indexPath.row];
    UITableViewCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:@"customIdentifier"];

    // Title should be a property on your MyObject class
    cell.textLabel.text = thisCellObject.title;
}

#pragma mark - UITableViewDelegate

// Implement the Delegate methods as you want it...
}