小编use*_*932的帖子

LiveData与Handler和LocalBroadcast

我有旧的Android/Java代码,它包含两个派生自IntentService,而这些服务不在单独的进程中运行.

问题是关于从这些结果返回结果的方法IntentService.

一个服务返回结果使用Handler+ Runnable,在主循环中运行代码:

new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() {
        MyApplication.get().setFoo(someThing);
    }
});
Run Code Online (Sandbox Code Playgroud)

另一个LocalBroadcastManager.getInstance(this).sendBroadcast(in);用于发送消息Activity,并Activity通过BroadcastReceiver消息onResume订阅,并取消订阅onPause.

我是对的,在这两种情况下都可以LiveData用来简化事情吗?

IntentService应该创建LiveData和谁想要结果observe,当新数据到达IntentService应该打电话postValue,或者可能有一些珊瑚礁,以防止在LiveData这里使用?

java android

18
推荐指数
1
解决办法
942
查看次数

为什么这个Rust程序这么慢?我错过了什么?

我读了曼哈顿的最小距离度量,并重写了作者在Rust中的"天真"实现.C++变体是:

#include <utility>
#include <cstdio>
#include <cstdlib>

std::pair<int, int> pointsA[1000001];
std::pair<int, int> pointsB[1000001];

int main() {
    int n, t;
    unsigned long long dist;

    scanf("%d", &t);

    while(t-->0) {
        dist = 4000000000LL;
        scanf("%d", &n);

        for(int i = 0; i < n; i++) {
            scanf("%d%d", &pointsA[i].first, &pointsA[i].second);
        }

        for(int i = 0; i < n; i++) {
            scanf("%d%d", &pointsB[i].first, &pointsB[i].second);
        }

        for(int i = 0; i < n ;i++) {
            for(int j = 0; j < n ; j++) …
Run Code Online (Sandbox Code Playgroud)

rust

17
推荐指数
2
解决办法
4037
查看次数

如何在宏中允许可选的尾随逗号?

这是我想要的合成例子:

macro_rules! define_enum {
    ($Name:ident { $($Variant:ident),* }) => {
        pub enum $Name {
            None,
            $($Variant),*,
        }
    }
}

define_enum!(Foo { A, B });
Run Code Online (Sandbox Code Playgroud)

此代码编译,但如果添加逗号:

define_enum!(Foo { A, B, });
//                     ^
Run Code Online (Sandbox Code Playgroud)

编译失败.我可以解决它:

($Name:ident { $($Variant:ident,)* })
//                             ^
Run Code Online (Sandbox Code Playgroud)

但后来define_enum!(Foo { A, B });失败了,

我应该如何编写一个宏来处理这两种情况:

define_enum!(Foo { A, B });
define_enum!(Foo { A, B, });
Run Code Online (Sandbox Code Playgroud)

macros rust

17
推荐指数
2
解决办法
1897
查看次数

为什么特质不能自我构建?

这段代码给我一个编译错误:

trait IBoo {
    fn new() -> Box<IBoo>;
}
Run Code Online (Sandbox Code Playgroud)

虽然此代码编译没有任何错误:

trait IBoo {
    //fn new() -> Box<IBoo>;
}

trait IFoo {
    fn new() -> Box<IBoo>;
}
Run Code Online (Sandbox Code Playgroud)
  1. 为什么第一个不能编译?rustc --explain E0038不直接暗示我为什么不可能.
  2. 是否可以在一个界面(特征)中组合构造和方法?

rust

13
推荐指数
2
解决办法
3255
查看次数

没有调用singleton的构造函数

我有单一类,旨在用于一个线程(GUI线程),以防止我添加的错误用法 assert

//header file
class ImageCache final {
public:
    ImageCache(const ImageCache &) = delete;
    ImageCache &operator=(const ImageCache &) = delete;
    static ImageCache &instance()
    {
       static ImageCache cache;
       return cache;
    }
    void f();
private:
    QThread *create_context_ = nullptr;
    ImageCache();
};

//cpp
ImageCache::ImageCache()
{
    create_context_ = QThread::currentThread();
    qInfo("begin, cur thread %p\n", create_context_);
}

void ImageCache::f()
{
    assert(create_context_ == QThread::currentThread());
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但在一台机器上有断言失败ImageCache::f,我没有直接访问该机器(因此这个问题).

有趣的是,根据日志ImageCache::ImageCache 根本没有被调用,断言因为失败而失败

assert(0 == QThread::currentThread());

我将ImageCache::instance头文件的实现移动到.cpp文件,将更新的源代码发送给这些机器的用户(在我的一切工作正常),他重建并且所有开始按预期工作.

我问他编译二进制文件(断言失败和没有),它们之间的唯一区别是ImageCache::instance实现的地方,

并比较汇编程序.

根本没有调用之间的区别ImageInstance::instance().f() …

c++ assembly c++11

13
推荐指数
1
解决办法
399
查看次数

WKWebView在后台,几个奇怪的断言

我想在 iOS 中找出当前浏览器的 userAgent。

所以在 Xcode 创建的默认项目中,我添加了:

#import "ViewController.h"
#import <WebKit/WKWebView.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    dispatch_async(dispatch_get_main_queue(), ^{
        WKWebView *obj = [[WKWebView alloc] initWithFrame:CGRectZero];
        [obj evaluateJavaScript: @"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
            NSLog(@"evaluateJavaScript = %@ : %@", result, error);
            NSLog(@"To make sure that webview alive: %@", obj);
        }];
    });
}


@end
Run Code Online (Sandbox Code Playgroud)

它打印出我期望的内容:

评估JavaScript = Mozilla/5.0 (iPad; CPU OS 14_0 like Mac OS …

objective-c ios

13
推荐指数
0
解决办法
6535
查看次数

未定义的静态constexpr引用

在此代码段中:

template <size_t N>
struct Foo {
   static constexpr std::array<char, N> arr{{0}};
   static const char *data() { return &arr[0]; }
};

template<>
constexpr std::array<char, 5> Foo<5>::arr;

int main()
{
   std::cout << Foo<5>::data() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

使用gcc 5.2我得到了未定义的引用Foo<5ul>::arr,而clang 3.7给出了编译时错误:

Constexpr静态数据成员'arr'的声明需要初始化程序

有什么不对,怎么static constexpr在课堂宣言之外定义?

c++ c++11

12
推荐指数
1
解决办法
3101
查看次数

是否可以在同一个匹配臂中组合两种模式,一种带有匹配防护装置?

我想检查字符串是否包含'$'以及'$'后面是否有内容:

我试过这段代码:

fn test(s: String) {
    match s.find('$') {
        None | (Some(pos) if pos == s.len() - 1) => {
          expr1();
        }
        _ => { expr2(); }
    }
}
Run Code Online (Sandbox Code Playgroud)

但它没有编译:

error: expected one of `)` or `,`, found `if`
Run Code Online (Sandbox Code Playgroud)

它是不可能的结合NoneSome比赛时手臂?

如果是这样,expr1()除非将其移动到单独的函数中,否则有一种简单的方法可以不复制?

rust

12
推荐指数
1
解决办法
453
查看次数

c ++如何从type_traits标准方式组合条件

例如,我想T只在它是std::is_pointer<T>和时使用类型std::is_const<T>.

当然,有这样简单的方法:

template <typename T>
void f(T t, std::true_type, std::true_type) {}
template <typename T>
void f(T t)
{
  f(t, std::is_pointer<T>{}, std::is_const<T>{});
}
Run Code Online (Sandbox Code Playgroud)

但是我想要这样的东西:

template <typename T>
void f(T t, std::true_type) {}
template <typename T>
void f(T t)
{
  f(t, std::and<std::is_pointer<T>, std::is_const<T>>{});
}
Run Code Online (Sandbox Code Playgroud)

在c ++标准类中是什么样的true_type?如果不是,是否有任何简单的方法来实现它,具有所需的功能?

c++ templates type-traits c++11

11
推荐指数
2
解决办法
2042
查看次数

当trait和struct使用相同的方法名时,如何调用方法?

该程序因无限递归而死亡:

use std::any::Any;

trait Foo {
    fn get(&self, index: usize) -> Option<&Any>;
}

impl Foo for Vec<i32> {
    fn get(&self, index: usize) -> Option<&Any> {
        Vec::get(self, index).map(|v| v as &Any)
    }
}

fn main() {
    let v: Vec<i32> = vec![1, 2, 4];
    println!("Results: {:?}", v.get(0))
}
Run Code Online (Sandbox Code Playgroud)

编译器本身警告:

warning: function cannot return without recurring
  --> src/main.rs:8:5
   |
8  |       fn get(&self, index: usize) -> Option<&Any> {
   |  _____^ starting here...
9  | |         Vec::get(self, index).map(|v| v as &Any)
10 | | …
Run Code Online (Sandbox Code Playgroud)

rust

11
推荐指数
1
解决办法
3342
查看次数

标签 统计

rust ×5

c++ ×3

c++11 ×3

android ×1

assembly ×1

ios ×1

java ×1

macros ×1

objective-c ×1

templates ×1

type-traits ×1