kaiwk/Tasker


Async(Chained)Task

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.Kermit95:Tasker:'
	}
	dependencies {
		implementation("com.github.Kermit95:Tasker:")
	}
	<dependency>
	    <groupId>com.github.Kermit95</groupId>
	    <artifactId>Tasker</artifactId>
	    <version></version>
	</dependency>

                            
    libraryDependencies += "com.github.Kermit95" % "Tasker" % ""
        
        

                            
    :dependencies [[com.github.Kermit95/Tasker ""]]
        
        

Readme


Async(Chained)Task

既然链式调用这么流行, 那么把AsyncTask也做成链式的好了。

核心流程:

可以看到实现方式非常naive, 如果加上lambda, 就会变成这样:

// 这个例子是无返回参数的任务
new Tasker()
        .justDo(() -> doSomeThing()).on(Task.ThreadMode.BACKGROUND_THREAD)
        .justDo(() -> updateUI()).on(Task.ThreadMode.UI_THREAD)
        .justDo(() -> complete()).on(Task.ThreadMode.UI_THREAD)
        .execute();

// 这个例子是有返回参数的任务
new Tasker(new TaskParam(0, "-_-||", 1, ":P"))
        .next(taskParam -> step1(taskParam)).on(Task.ThreadMode.BACKGROUND_THREAD)
        .next(taskParam -> step2(taskParam)).on(Task.ThreadMode.BACKGROUND_THREAD)
        .next(taskParam -> step3(taskParam)).on(Task.ThreadMode.UI_THREAD)
        .execute();