小编ske*_*ell的帖子

当使用includes进行预缓存时,Rails会忽略关联范围

TL; DR:这个问题在https://github.com/skensell/SO-question-example有自己的示例应用程序,您可以使用它自行调试.我已经在这个问题上提了一次赏金,但我不相信(或者我不明白)最重要的回答者的推理.我将再次给予此奖励,因为这让我感到非常沮丧.

原始问题

我有一个User像这样的关联的模型:

has_many :avatars, -> { order([:sort_order => :asc,:created_at => :asc])}
Run Code Online (Sandbox Code Playgroud)

我有一个端点,它执行搜索用户并设置@users视图使用的变量.这是我在调试器中找到的怪异部分:

@users.first.avatars[0..2].map(&:id)
# => [2546, 2547, 2548]
# This is the correct order.

@users.to_a.first.avatars[0..2].map(&:id)
# => [2548, 2546, 2547]
# Wrong order.
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?

唯一的区别是to_a.我甚至试图忽略它to_a,但我认为它被jbuilder隐含地调用,因为我将它设置为json数组.

也许我正在搜索的方式User与它有关?我正在使用几个包含和连接.

UPDATE

在这里,我可以向您展示rails控制台中这种奇怪行为的简单示例.似乎包括..参考是罪犯,但我不明白为什么或如何.

User.order(id: :desc)
    .includes(:avatars, :industries)
    .where(industries: {id:  [5]})
    .references(:industries)
    .limit(5).to_a.second.avatars.map(&:id)
# => [2751, 2748, 2749]
# Wrong order.

User.order(id: :desc)
    .includes(:avatars, :industries)
    .where(industries: {id:  [5]})
    .references(:industries)
    .limit(5).second.avatars.map(&:id)
# …
Run Code Online (Sandbox Code Playgroud)

ruby activerecord ruby-on-rails

12
推荐指数
2
解决办法
1048
查看次数

为什么 UIPopoverPresentationController 调整其内容视图的大小?

我尝试过使用旧的(iOS 7)和新的(iOS 8)方式在弹出窗口中呈现视图控制器,但是当我的 VC 在弹出窗口中呈现时,它的侧面有一些额外的空间,因为它的框架大小被设置为宽 13 点。为什么?

更多细节

它在 iOS7 模拟器中看起来很好(当用旧方式编写时)。

我意识到 13 pt 可能是箭头的大小(当您强制箭头向上或向下时,它会将 13 pt 添加到高度而不是宽度)。

我做了一些进一步的检查(通过在我的视图中设置断点setFrame:),发现有一个UIPopoverPresentationController方法presentationTransitionWillBegin调用了setFrame:错误的框架。阅读UIPopoverPresentationController 类参考后,我了解到该类的实例是UIKit在呈现弹出窗口时创建和管理的。那么,UIKit你为什么要尝试调整我的视图大小以包含箭头?

以供参考

通过“旧方式”,我的意思是我重写contentSizeForViewInPopover正在呈现的视图控制器的方法,然后将其呈现

self.popoverController = [[UIPopoverController alloc] initWithContentViewController:aViewController];
[self.popoverController presentPopoverFromRect:rect
                                        inView:anInView
                      permittedArrowDirections:popoverArrowDirection
                                      animated:animated];
Run Code Online (Sandbox Code Playgroud)

我所说的“新方式”是指

viewController.preferredContentSize = CGSizeMake(50.0f, 50.0f);
viewController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:viewController animated:YES completion:nil];

UIPopoverPresentationController *presentationController = [viewController popoverPresentationController];
presentationController.permittedArrowDirections = UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight;
presentationController.sourceView = aSourceView;
presentationController.sourceRect = aFrame;
Run Code Online (Sandbox Code Playgroud)

我目前的解决方法

我不允许我的视图被UIPopoverPresentationController …

objective-c popover uipopovercontroller ios

5
推荐指数
0
解决办法
2250
查看次数

刷新领域的成本是多少?

我已阅读文档和理解,在很多情况下,你不需要手动调用refreshRealm实例.但是,在这个非常常见的场景中,它被证明是必要的,因为完成块可能会在下一个运行循环开始之前查询Realm.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [[RLMRealm defaultRealm] transactionWithBlock:^{
        // Add some RLMObjects
    }];

    if (completion) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[RLMRealm defaultRealm] refresh]; // necessary if it queries realm
            completion();
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

我认为通过在后台线程上执行写操作我是一个好公民,但现在我必须打电话refresh,我想知道这个调用所涉及的开销是否违背了后台处理的要点.

所以我的问题是:

1)什么是调用的性能开销refreshRealm

2)在这种模式中只向一个领域添加一个对象可能毫无意义.在这个模式中添加多少个对象后,我会看到一个优势,而不是只是同步在主线程上执行写入事务?

multithreading realm

3
推荐指数
1
解决办法
447
查看次数