我有旧的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
这里使用?
我读了曼哈顿的最小距离度量,并重写了作者在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) 这是我想要的合成例子:
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) 这段代码给我一个编译错误:
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)
rustc --explain E0038
不直接暗示我为什么不可能.我有单一类,旨在用于一个线程(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() …
我想在 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 …
在此代码段中:
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
在课堂宣言之外定义?
我想检查字符串是否包含'$'以及'$'后面是否有内容:
我试过这段代码:
fn test(s: String) {
match s.find('$') {
None | (Some(pos) if pos == s.len() - 1) => {
expr1();
}
_ => { expr2(); }
}
}
Run Code Online (Sandbox Code Playgroud)
但它没有编译:
Run Code Online (Sandbox Code Playgroud)error: expected one of `)` or `,`, found `if`
它是不可能的结合None
和Some
比赛时手臂?
如果是这样,expr1()
除非将其移动到单独的函数中,否则有一种简单的方法可以不复制?
例如,我想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
?如果不是,是否有任何简单的方法来实现它,具有所需的功能?
该程序因无限递归而死亡:
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)