File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
src/docs/product/issues/issue-details/performance-issues/regex-main-thread Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -62,3 +62,33 @@ DispatchQueue.global(qos: .userInitiated).async {
62
62
}
63
63
}
64
64
```
65
+
66
+ ### Android
67
+
68
+ The following code calls ` String.matches ` on the UI thread to search for matches against a regular expression in a large text:
69
+
70
+ ``` kotlin
71
+ fun matchRegex (text : String , regexPattern : String ) {
72
+ val matches = text.matches(regexPattern.toRegex())
73
+ if (matches) {
74
+ // Do something
75
+ }
76
+ }
77
+ ```
78
+
79
+ Performance could be improved by moving the match to a coroutine:
80
+
81
+ ``` kotlin
82
+ fun matchRegex (text : String , regexPattern : String ) {
83
+ lifecycleScope.launch(Dispatchers .Default ) {
84
+ val pattern = Pattern .compile(regexPattern)
85
+ val matches = pattern.matcher(text).matches()
86
+
87
+ withContext(Dispatchers .Main ) {
88
+ if (matches) {
89
+ // Do something
90
+ }
91
+ }
92
+ }
93
+ }
94
+ ```
You can’t perform that action at this time.
0 commit comments