菜鸟教程小白 发表于 2022-12-12 03:56:29

ios - 在 Restkit 0.2 中加载具有相同实体和不同谓词的两个不同表时遇到问题


                                            <p><p>这是我正在尝试做的简化版本:</p>

<p>我有一个使用 UITabBarController 作为根 Controller 的应用程序。
在两个选项卡中,我有一个 UINavigationController,其中包含一个自定义 UITableViewController。</p>

<p>其中一个表格 View 用于显示“特色”项目,另一个用于显示“用户”项目。</p>

<p>第一个 API 端点是:/api/items/featured
另一个端点是:/api/items/user</p>

<p>我在我的 AppDelegate.m 中完成了设置所有内容的整个过程:</p>

<pre><code>// Configure the object manager
RKObjectManager *objectManager = ];
objectManager.managedObjectStore = managedObjectStore;
;

RKEntityMapping *entityMapping = ;
[entityMapping addAttributeMappingsFromDictionary:@{
@&#34;id&#34;:                     @&#34;itemID&#34;,
@&#34;type&#34;:                   @&#34;type&#34;,
@&#34;created_at&#34;:             @&#34;created_at&#34;:
@&#34;field1&#34;:               @&#34;field1&#34;}];

// User Descriptor
RKResponseDescriptor *userDescriptor = ;

;

// Featured Listing Decriptor
RKResponseDescriptor *featuredDescriptor = ;

;
</code></pre>

<p>然后在 TableViewControls 中我有以下内容(在第二个 tableviewController 中用 'user' 代替 'featured')</p>

<pre><code>- (void)viewDidLoad {
    ;
    // Do any additional setup after loading the view, typically from a nib.

    ;
}

- (void)loadItems {   
    [ getObjectsAtPath:@&#34;/api/items/featured&#34; parameters:nil
      success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    self.lastupdate = [ init];
    }
      failure:^(RKObjectRequestOperation *operation, NSError *error) {
    UIAlertView *alertView = [ initWithTitle:@&#34;An Error Has Occurred&#34; message: delegate:nil cancelButtonTitle:@&#34;OK&#34; otherButtonTitles:nil];
    ;
    }];
}

- (NSFetchedResultsController *)fetchedResultsController {
    if (_fetchedResultsController != nil) {
      return _fetchedResultsController;
    }
    RKManagedObjectStore *managedObjectStore = ;
    self.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;

    NSFetchRequest *fetchRequest = [ init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = ;

    ;

    // Set the batch size to a suitable number.
    ;

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [ initWithKey:@&#34;created_at&#34; ascending:NO];
    NSArray *sortDescriptors = @;

    ;

    // Set predicate to only get Featured Listings
    NSPredicate *predicate = ;
    ;

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means &#34;no sections&#34;.
    NSFetchedResultsController *aFetchedResultsController = [ initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@&#34;Master&#34;];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;

    if (!) {
      // Replace this implementation with code to handle the error appropriately.
      // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
      NSLog(@&#34;Unresolved error %@, %@&#34;, error, );
      abort();
    }

    return _fetchedResultsController;
}
</code></pre>

<p>现在这一切都适用于第一个选项卡(目前它返回 8 个项目,type = 'featured',显示在表格中)。</p>

<p>当我切换到第二个标签时,问题就来了。它目前只返回 1 个 type='user' 的项目,并崩溃:</p>

<blockquote>
<p>CoreData: error: Serious application error.Exception was caught during Core Data change processing.This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.<em>*</em> -: index 3 beyond bounds for empty array with userInfo (null)</p>
</blockquote>

<p>如果我在“用户”选项卡上省略谓词,则“特色”选项卡可以正常工作,“用户”选项卡会在其表中获取“特色”项目,而“用户”项目在顶部。</p>

<p>由于两个 tableview 控件的代码是相同的,除了“用户”和“功能”,我不知道问题出在哪里。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当您创建和配置获取的结果 Controller 时,不要使用缓存。目前他们都在使用 <code>@"Master"</code> 的缓存,这将导致他们尝试共享不合适的信息,因为获取请求不同。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 Restkit 0.2 中加载具有相同实体和不同谓词的两个不同表时遇到问题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15890674/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15890674/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 Restkit 0.2 中加载具有相同实体和不同谓词的两个不同表时遇到问题