小编Yan*_*rin的帖子

MySQL 不更新 information_schema,除非我手动运行 ANALYZE TABLE `myTable`

我需要获取表 (InnoDB) 的最后一个 id(主键),为此我执行以下查询:

SELECT (SELECT `AUTO_INCREMENT` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = 'mySchema' AND `TABLE_NAME` = 'myTable') - 1;
Run Code Online (Sandbox Code Playgroud)

它返回错误的 AUTO_INCREMENT。问题是 information_schema 的 TABLES 表未使用当前值更新,除非我运行以下查询:

ANALYZE TABLE `myTable`;
Run Code Online (Sandbox Code Playgroud)

为什么 MySQL 不自动更新 information_schema,我该如何解决这个问题?
运行 MySQL 服务器 8.0.13 X64。

mysql innodb information-schema mysql-8.0

9
推荐指数
1
解决办法
5604
查看次数

使用androidx.preference.PreferenceScreen和PreferenceScreen之间的区别

我的应用定位到API 28并具有最低要求API 15。作为支持库,我正在使用AndroidX
我有一个活动托管的首选项片段,如下所示:

SettingsFragment.java

package com.example.app;

import android.os.Bundle;

import androidx.preference.PreferenceFragmentCompat;

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preferences, rootKey);

    }
}
Run Code Online (Sandbox Code Playgroud)

SettingsActivity.java

package com.example.app;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 SettingsFragment.java

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <SwitchPreference
        android:defaultValue="false"
        android:key="pref_switch"
        android:title="@string/switch" />
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

作为首选项层次结构的根,我应该使用PreferenceScreen还是androidx.preference.PreferenceScreen使布局实际上向后兼容(使用AndroidX)?两者有什么区别?最佳做法是什么?

android android-preferences sharedpreferences android-xml androidx

5
推荐指数
1
解决办法
5909
查看次数

NotificationService:Toast 已经被杀死

当从 AIDL 服务回调中调用 show toast 消息时,toast 会闪烁。我runOnUiThread()在 AIDL 回调中尝试使用、处理程序等...但仍然没有解决。

有人遇到过类似的问题吗?

android android-toast

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

使用架构组件导航将数据从单个活动传递到起始目标片段

我有以下内容Activity,这是我的应用程序中的一个:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        PermissionState state = PermissionState.get(this);
        if (state == PermissionState.GRANTED) {
            getWindow().setBackgroundDrawable(new ColorDrawable(-1));
        }

        super.onCreate(savedInstanceState);

        ActivityMainBinding mainActivityBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        setSupportActionBar(mainActivityBinding.toolbarMain);
        NavigationUI.setupWithNavController(mainActivityBinding.toolbarMain, Navigation.findNavController(this, R.id.nav_host_fragment));
    }
}
Run Code Online (Sandbox Code Playgroud)

与关联的布局Activity如下,是根据 Android开发者教程创建的。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </com.google.android.material.appbar.AppBarLayout>

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph" />

    </LinearLayout>

</layout>
Run Code Online (Sandbox Code Playgroud)

所述 …

android android-navigation android-architecture-components android-jetpack android-architecture-navigation

5
推荐指数
2
解决办法
2428
查看次数

用于 Xamarin.Forms 的 Prism 上的 DryIoc 和 IServiceProvider (DryIoc.Microsoft.DependencyInjection)

我有一个以 DryIoc 作为容器的 Prism 应用程序。我想向我的类型客户IHttpClientFactory提供HttpClients ,它们是这样的:

public class ExampleService : IExampleService
{
    private readonly HttpClient _httpClient;

    public RepoService(HttpClient client)
    {
        _httpClient = client;
    }

    public async Task<IEnumerable<string>> GetExamplesAsync()
    {
        // Code deleted for brevity.
    }
}
Run Code Online (Sandbox Code Playgroud)

App.xaml.cs 中,我注册了我的类型化客户端,以便它们可以使用以下内容注入到视图模型中:

public partial class App

// ...

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    // Code deleted for brevity.
    containerRegistry.Register<IExampleService, ExampleService>();
}
Run Code Online (Sandbox Code Playgroud)

那是在尝试使用IHttpClientFactory.

现在,要添加它,我们应该 AddHttpClient()IServiceCollection. 这就是我认为需要DryIoc.Microsoft.DependencyInjection 的地方,所以,仍然在App.xaml.cs 中 …

prism xamarin.forms

4
推荐指数
1
解决办法
1290
查看次数

如何在后台线程上运行ListenableWorker工作?

由于我需要在WorkManager中异步执行工作,因此我需要使用ListenableWorker,默认情况下,它在主(UI)线程上运行。由于这项工作可能是很长时间的处理任务,可能会冻结接口,因此我想在后台线程上执行它。在使用WorkManager(Android Dev Summit '18)视频中,Google工程师展示了如何手动配置WorkManager以在自定义上运行作品Executor,因此我遵循他的指导:

1)在AndroidManifest中禁用默认的WorkManager初始化程序:

<provider
    android:name="androidx.work.impl.WorkManagerInitializer"
    android:authorities="com.example.myapp.workmanager-init"
    tools:node="remove" />
Run Code Online (Sandbox Code Playgroud)

2)在Application.onCreate中,使用自定义配置初始化WorkManager,在我的情况下是这样的:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        Configuration configuration = new Configuration.Builder().setExecutor(Executors.newSingleThreadExecutor()).build();
        WorkManager.initialize(this, configuration);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我的实际ListenableWorker是这样的:

@NonNull
    @Override
    public ListenableFuture<Result> startWork() {
        Log.d(TAG, "Work started.");
        mFuture = ResolvableFuture.create();
        Result result = doWork();
        mFuture.set(result);
        return mFuture;
    }

    private Result doWork() {
        Log.d(TAG, "isMainThread? " + isMainThread());
        mFusedLocationProviderClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if …
Run Code Online (Sandbox Code Playgroud)

performance android background android-architecture-components android-workmanager

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

从 IEnumerable&lt;Task&lt;T&gt;&gt; 到 IAsyncEnumerable&lt;T&gt; 通过在 Parallel.ForEach/Parallel.ForEachAsync 内返回的yield 给出错误 CS1621

在 .NET 6 项目中,我必须调用一个偏移分页(页/每页)的 Web API,并且我希望尽可能使 n 个调用并行。

这是使用给定页码调用 API 一次的方法:

private Task<ApiResponse> CallApiAsync(int page,
    CancellationToken cancellationToken = default)
{
    return GetFromJsonAsync<ApiResponse>($"...&page={page}", cancellationToken)
        .ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud)

我实际上需要的是从第 1 页到第 n 页的所有 API 调用的仅前向流式迭代器,因此考虑到这一要求,我认为这IAsyncEnumerable是正确的 API,这样我就可以并行触发 API 调用并访问每个 API 响应一旦准备好,就可以完成,而不需要全部完成。

所以我想出了以下代码:

public async IAsyncEnumerable<ApiResponse> CallApiEnumerableAsync(int perPage,
    [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
    int numProducts = GetNumberOfProducts(perPage);

    int numCalls = MathExtensions.CeilDiv(numProducts, perPage);

    var pages = Enumerable.Range(1, numCalls);

    Parallel.ForEach(pages, async page => {
        yield return await CallApiAsync(page, cancellationToken).ConfigureAwait(false);
    });

    yield break;
}
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误 …

c# parallel.foreach .net-core iasyncenumerable parallel.foreachasync

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